更改单元格中的值,如果单元格的开头和末尾分别有-*和*



我有一个像这样的数据帧

Name     salary    age    Id
Steve    23000    -*29*   22
Mark    -*2900*    24     82
John     40000     26   -*72*

I would like

Name     salary    age    Id
Steve    23000     29     22
Mark     2900      24     82
John     40000     26     72

我试过了。

df.apply(lambda x:x[2:-1] if x.str.startwith('="') and x.str.endwith('"') else x)

我得到错误

ValueError: The truth of a series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

我认为我没有到达单元格,而是将更改应用于列。

IIUC,您想要删除-*字符:

df = df.apply(lambda c: c.str.strip('-*'))

输出:

Name salary age  Id
0  Steve  23000  29  22
1   Mark   2900  24  82
2   John  40000  26  72

NB。您的单元格仍将是字符串,以转换为数字:

df = df.apply(lambda c: pd.to_numeric(c.str.strip('-*'), errors='ignore'))

相关内容

最新更新