如何从熊猫数据帧中的条件下的字符串列表中获取子字符串
这就是我所拥有的:
df = pd.DataFrame({'Name': {0: 'Mark', 1: 'John', 2: 'Rick'},
'Location': {0: ['Mark lives in UK',
'Rick lives in France',
'John Lives in US'],
1: ['Mark lives in UK', 'Rick lives in France', 'John Lives in US'],
2: ['Mark lives in UK', 'Rick lives in France', 'John Lives in US']}})
这就是我想要得到的:
desired_output = pd.DataFrame({'Name': ['Mark', 'John', 'Rick'],
'Location':[['Mark lives in UK', 'Rick lives in France', 'John Lives in US'], ['Mark lives in UK', 'Rick lives in France', 'John Lives in US'], ['Mark lives in UK', 'Rick lives in France', 'John Lives in US']],
'Outcome': ['Mark lives in UK', 'John Lives in US', 'Rick lives in France']
})
这就是我尝试的:
df['Sorted'] = df['Location'].str.split(',')
df.apply(lambda x: [idx for idx,s in enumerate(x.sorted) if x.Name in x.sorted])
事先感谢您!
This is what I have:
df = pd.DataFrame({'Name': {0: 'Mark', 1: 'John', 2: 'Rick'},
'Location': {0: ['Mark lives in UK',
'Rick lives in France',
'John Lives in US'],
1: ['Mark lives in UK', 'Rick lives in France', 'John Lives in US'],
2: ['Mark lives in UK', 'Rick lives in France', 'John Lives in US']}})
This is what I'd like to get:
desired_output = pd.DataFrame({'Name': ['Mark', 'John', 'Rick'],
'Location':[['Mark lives in UK', 'Rick lives in France', 'John Lives in US'], ['Mark lives in UK', 'Rick lives in France', 'John Lives in US'], ['Mark lives in UK', 'Rick lives in France', 'John Lives in US']],
'Outcome': ['Mark lives in UK', 'John Lives in US', 'Rick lives in France']
})
Here is what I tried:
df['Sorted'] = df['Location'].str.split(',')
df.apply(lambda x: [idx for idx,s in enumerate(x.sorted) if x.Name in x.sorted])
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您从一开始就不需要位置列,则可以使用以下方式:
如果您真的需要该中间列,则可以执行此操作并重新名称列
If you don't need the Location column from the beginning you can use this:
If you really need that middle column you can do this and re-name the columns
您可以在行上尝试
应用
,也可以尝试
爆炸
You can try
apply
on rowsOr you can try
explode