python - 如何遍历列并在 Pandas 中只返回一个变量
问题描述
我一直在尝试将 DataFrame 中的列与列表进行比较并找出差异。唯一的问题是,当我这样做时,它会给出差异列表,但会为每一列添加一个条目。
例如:
def Type():
list1=['Dog','Cat','Monkey','Horse', 'Giraffe']
list2=Zoo['Animal'].values.tolist()
new_list = (set(list1).difference(set(list2)))
return new_list
Zoo['Animal Type'] = Zoo.apply(lambda row: Type(), axis = 1)
Zoo['Animal Type']
返回
34 {Cat, Monkey, Dog, Horse}
38 {Cat, Monkey, Dog, Horse}
39 {Cat, Monkey, Dog, Horse}
41 {Cat, Monkey, Dog, Horse}
46 {Cat, Monkey, Dog, Horse}
47 {Cat, Monkey, Dog, Horse}
48 {Cat, Monkey, Dog, Horse}
49 {Cat, Monkey, Dog, Horse}
而不是最后的差异列表,例如:
0 Cat
1 Monkey
2 Dog
3 Horse
我只希望每个差异在输出中说明一次。任何帮助都是极好的。
解决方案
推荐阅读
- javascript - 在使用 if 语句暂停之前,数字会增加吗?
- javascript - Typescript - 在另一个对象中检索一个对象
- javascript - 在javascript中匹配指定的Regex后如何在单独的变量中获取字符串
- android - kivy android AttributeError:'NoneType'对象没有属性'play'
- c++ - CMake 管理本地依赖项
- c# - 获取每个 xml 节点的 XmlNode 值
- python - Gensim 的相似之处
- python - 如何将传感器数据写入单独的文本文件(Raspberry Pi,Python)
- javascript - 引导定价示例 - 我的代码做错了什么?
- dask - 在 PythonScriptStep 中使用 Dask 集群