[英]Replacing Special Symbols in Pandas Dataframe
Can anyone help me to remove extra characters from the dataframe column?谁能帮我从 dataframe 列中删除多余的字符? Should I have to use
replace
string method?我应该使用
replace
字符串方法吗?
For example,例如,
`$7.99` -> `7.99`
`$16.99` -> `16.99`
`$0.99` -> `0.99`
IIUC: consider the following dataframe: IIUC:考虑以下 dataframe:
df = pd.DataFrame(['$7.99', '$3.45', '$56.99'])
you can use replace
to do:你可以使用
replace
来做:
df[0].str.replace('$', '', regex=False)
Output: Output:
0 7.99
1 3.45
2 56.99
Name: 0, dtype: object
You can remove the first character with .str[1:]
:您可以使用
.str[1:]
删除第一个字符:
df["Column1"] = df["Column1"].str[1:].astype(float)
print(df)
Prints:印刷:
Column1
0 7.99
1 16.99
2 0.99
Dataframe used: Dataframe 使用:
Column1
0 $7.99
1 $16.99
2 $0.99
You can use pandas.DataFrame.mask to replace certain values, for example, as first positional parameter I provide a conditonal that is used in df.mask(df.str[0]=='$','')
as if-them conditional.您可以使用pandas.DataFrame.mask替换某些值,例如,作为第一个位置参数,我提供了一个在
df.mask(df.str[0]=='$','')
中使用的条件,好像-他们是有条件的。
df.mask(df.str[0]=='$',df.str[1:])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.