我需要从列中删除第一个单词,有时还需要删除第一个和第二个单词,以便可以合并两个数据帧。例如,在一个数据框中,“球队”列的格式为凯尔特人、掘金队、湖人队。在我的第二个数据框中,“团队”列的格式为波士顿凯尔特人、丹佛掘金、洛杉矶湖人。我遇到了麻烦,因为一些球队,比如湖人和鲸鱼,在实际的球队名称“湖人”之前有两个词(洛杉矶湖人,新奥尔良)。我使用的是Python和Pandas。Here is an example in List Format of what the column 'Teams' looks like in the data frame I need to manipulate
我尝试了@Nk03解决方案
并收到屏幕截图中的输出。我是Python的新手,所以我仍然对为什么解决方案不起作用感到困惑。
发布于 2021-04-28 03:56:15
尝尝这个。-
这里的想法是提取团队名称的一部分,如果该部分出现在第一个数据帧中-
def extract_team(x):
for item in x.split():
if item in df1.Teams.values:
return item
return x
df2.Teams = df2.Teams.apply(lambda x : extract_team(x))
https://stackoverflow.com/questions/67289592
复制相似问题