【发布时间】:2022-09-23 17:29:03
【问题描述】:
谁能帮我从数据框列中删除多余的字符?我应该使用replace 字符串方法吗?
例如,
`$7.99` -> `7.99`
`$16.99` -> `16.99`
`$0.99` -> `0.99`
-
df[\'col\'] = df[\'col\'].str.lstrip(\'$\') -
你可能想添加
.astype(float)
标签: python pandas string replace
谁能帮我从数据框列中删除多余的字符?我应该使用replace 字符串方法吗?
例如,
`$7.99` -> `7.99`
`$16.99` -> `16.99`
`$0.99` -> `0.99`
df[\'col\'] = df[\'col\'].str.lstrip(\'$\')
.astype(float)
标签: python pandas string replace
IIUC:考虑以下数据框:
df = pd.DataFrame(['$7.99', '$3.45', '$56.99'])
你可以使用replace 来做:
df[0].str.replace('$', '', regex=True)
输出:
0 7.99
1 3.45
2 56.99
Name: 0, dtype: object
【讨论】:
您可以使用.str[1:] 删除第一个字符:
df["Column1"] = df["Column1"].str[1:].astype(float)
print(df)
印刷:
Column1
0 7.99
1 16.99
2 0.99
使用的数据框:
Column1
0 $7.99
1 $16.99
2 $0.99
【讨论】: