String 删除“;x";数据帧中字符串的字符数?
我有一个熊猫数据帧df,如下所示:String 删除“;x";数据帧中字符串的字符数?,string,python-3.x,pandas,String,Python 3.x,Pandas,我有一个熊猫数据帧df,如下所示: a b thisisastring 5 anotherstring 6 thirdstring 7 我想根据b列中的数字删除a列中字符串左侧的字符。所以我试着: df["a"] = d["a"].str[df["b"]:] 但这将导致: a b NaN 5 NaN 6 NaN 7
a b
thisisastring 5
anotherstring 6
thirdstring 7
我想根据b列中的数字删除a列中字符串左侧的字符。所以我试着:
df["a"] = d["a"].str[df["b"]:]
但这将导致:
a b
NaN 5
NaN 6
NaN 7
而不是:
a b
sastring 5
rstring 6
ring 7
有什么帮助吗?提前谢谢 您可以使用
apply
执行此操作,以便按行应用此命令:
df.apply(lambda x: x.a[x.b:],axis=1)
0 sastring
1 rstring
2 ring
dtype: object
将
zip
与字符串片段一起使用
df.a=[x[y:] for x,y in zip(df.a,df.b)]
df
Out[584]:
a b
0 sastring 5
1 rstring 6
2 ring 7
当我将它应用到我的数据集时,这一个肯定更快!非常感谢!