我有一个数据框,我想在其中连接某些列。
我的问题是这些列中的文本可能包含也可能不包含重复信息。我想删除重复项以便仅保留相关信息。
例如,如果我有一个数据框,例如:
pd.read_csv("animal.csv")
animal1 animal2 label
1 cat dog dolphin 19
2 dog cat cat 72
3 pilchard 26 koala 26
4 newt bat 81 bat 81
我想合并列,但只保留每个字符串的唯一信息。
您可以看到在第 2 行中,“猫”同时包含在“动物 1”和“动物 2”列中。在第 3 行中,数字 26 在“Animal1”和“Label”列中。而在第 4 行中,“Animal2”和“Label”列中的信息已按顺序包含在“Animal1”中。
我通过执行以下操作合并列
animals["detail"] = animals["animal1"].map(str) + animals["animal2"].map(str) + animals["label"].map(str)
animal1 animal2 label detail
1 cat dog dolphin 19 cat dog dolphin 19
2 dog cat cat 72 dog cat cat 72
3 pilchard 26 koala 26 pilchard 26 koala 26
4 newt bat 81 bat 81 newt bat 81 bat 81
第 1 行没问题,但其他行当然包含如上所述的重复项。
我想要的输出是:
animal1 animal2 label detail
1 cat dog dolphin 19 cat dog dolphin 19
2 dog cat cat 72 dog cat 72
3 pilchard 26 koala 26 pilchard koala 26
4 newt bat 81 bat 81 newt bat 81
或者如果我只能保留详细信息列中每行每个单词/数字的第一个唯一实例,这也是合适的,即:
detail
1 cat dog dolphin 19
2 dog cat 72
3 pilchard koala 26
4 newt bat 81
我看过在 python 中对字符串执行此操作,例如How can I remove duplicate words in a string with Python? , How to get all the unique words in the data frame? , show distinct column values in pyspark dataframe: python 但无法弄清楚如何将其应用于详细信息列中的各个行。我已经考虑过在合并列之后拆分文本,然后使用 apply 和 lambda,但还没有让它起作用。或者在合并列时是否有办法做到这一点?
我有 solution in R但想用 python 重新编码。
非常感谢任何帮助或建议。我目前正在使用 Spyder(Python 3.5)
请您参考如下方法:
您可以添加自定义函数,首先按空格分隔,然后按 pandas.unique 获取唯一值最后加入字符串返回:
animals["detail"] = animals["animal1"].map(str) + ' ' +
animals["animal2"].map(str) + ' ' +
animals["label"].map(str)
animals["detail"] = animals["detail"].apply(lambda x: ' '.join(pd.unique(x.split())))
print (animals)
animal1 animal2 label detail
1 cat dog dolphin 19 cat dog dolphin 19
2 dog cat cat 72 dog cat 72
3 pilchard 26 koala 26 pilchard 26 koala
4 newt bat 81 bat 81 newt bat 81
也可以在 apply 中加入值:
animals["detail"] = animals.astype(str)
.apply(lambda x: ' '.join(pd.unique(' '.join(x).split())),axis=1)
print (animals)
animal1 animal2 label detail
1 cat dog dolphin 19 cat dog dolphin 19
2 dog cat cat 72 dog cat 72
3 pilchard 26 koala 26 pilchard 26 koala
4 newt bat 81 bat 81 newt bat 81
设置的解决方案,但它改变了顺序:
animals["detail"] = animals.astype(str)
.apply(lambda x: ' '.join(set(' '.join(x).split())), axis=1)
print (animals)
animal1 animal2 label detail
1 cat dog dolphin 19 cat dolphin 19 dog
2 dog cat cat 72 cat dog 72
3 pilchard 26 koala 26 26 pilchard koala
4 newt bat 81 bat 81 bat 81 newt
