我试图移除列中特定位置的特定字符。有什么办法可以用条形定位型函数来做吗?感谢任何支持!!提前谢谢!!
样本df
Report
RPi-1
RPi-2
RPi-3
RPi-4我想删除报告列中每一行中字符串位置3处的“i”。
期望df
Report
RP-1
RP-2
RP-3
RP-4发布于 2020-05-27 04:16:41
df['Report'] = df['Report'].str.replace('RPi', 'RP')或
df["Report"]=df["Report"].str.replace("i","")希望这能帮到你
发布于 2020-05-27 04:17:10
这个很管用。您可以将"i“替换为任何要替换的任意字符。
df[col]=df[col].apply(lambda x: x.replace("i", ""))
发布于 2020-05-27 04:25:10
如果要在特定位置替换字符,可以使用Series.str进行替换。
df.Report = df.Report.replace(df.Report.str[2], '') ## str[2] gives 3rd character输出:
Report
0 RP-1
1 RP-2
2 RP-3
3 RP-4https://stackoverflow.com/questions/62035305
复制相似问题