我有这样的df,
Main Length
Sri playnig well cricket 5
sri went out 2
Ram is in 1
Ram went to UK,US 2
我正在尝试切片 df["Main"] based on df["Length"]
我的预期输出是
Main Length
Sri p 5
sr 2
R 1
Ra 2
我试过了
def slicer(row):
for i in df["Length"]:
row['Main'].slice(0,i)
return row
df.apply(slicer,axis=1)
但我得到了,AttributeError: ("'str' object has no attribute 'slice'", 'occurred at index 0')
请帮忙。
apply
与索引一起使用:
df['Main'] = df.apply(lambda x: x['Main'][:x['Length']], axis=1)
或列出理解,zip
如果没有NaN
s值:
df['Main'] = [a[:b] for a, b in zip(df['Main'], df['Length'])]
print(df)
Main Length
0 Sri p 5
1 sr 2
2 R 1
3 Ra 2
对于更通用的解决方案,可以使用if-else
:
df['Main'] = [a[:b] if len(a) < b else a for a, b in zip(df['Main'], df['Length'])]
print(df)
Main Length
0 Sri playnig well cricket 100
1 sri went out 2
2 Ram is in 1
3 Ram went to UK,US 2
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句