Pandas - 如何在 DataFrame 系列中用零值替换字符串? [英] Pandas - How to replace string with zero values in a DataFrame series?

查看:124
本文介绍了Pandas - 如何在 DataFrame 系列中用零值替换字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些 csv 数据导入 Pandas DataFrame(在 Python 中).一个系列意味着所有数值.但是,它还包含一些以字符串表示的虚假$-"元素.这些是从以前的格式中遗留下来的.如果我只是导入该系列,Pandas 会将其报告为一系列对象".

I'm importing some csv data into a Pandas DataFrame (in Python). One series is meant to be all numerical values. However, it also contains some spurious "$-" elements represented as strings. These have been left over from previous formatting. If I just import the series, Pandas reports it as a series of 'object'.

用零替换这些$-"字符串的最佳方法是什么?或者更一般地说,我如何用数值替换一个系列(主要是数字)中的所有字符串,并将该系列转换为浮点类型?

What's the best way to replace these "$-" strings with zeros? Or more generally, how can I replace all the strings in a series (which is predominantly numerical), with a numerical value, and convert the series to a floating point type?

  • 史蒂夫

推荐答案

使用 Series.str.replaceSeries.astype

df = pd.Series(['2$-32$-4','123$-12','00123','44'])
df.str.replace(r'\$-','0').astype(float)

0    203204
1    123012
2       123
3        44
dtype: float64

这篇关于Pandas - 如何在 DataFrame 系列中用零值替换字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆