简体   繁体   English

AttributeError: 'set' 对象没有属性 'to_csv'

[英]AttributeError: 'set' object has no attribute 'to_csv'

Good day everyone!今天是个好日子! I have 2 files txt and csv with numbers and I would like to compare and delete from 1st file numbers matching with a numbers in second file.我有 2 个带有数字的文件 txt 和 csv,我想从与第二个文件中的数字匹配的第一个文件号中进行比较和删除。 Data and df_row are Datagrams. Data 和 df_row 是数据报。

For example: my first file consist例如:我的第一个文件包括

12354564
25345754
23545454
11565654
46456456

and my second file consist我的第二个文件包括

23545454
11565654
46456456

so result should be所以结果应该是

12354564
25345754

My code:我的代码:

result = set(data).difference(set(df_row))
result.to_csv("part1left.txt")

but there is an error AttributeError: 'set' object has no attribute 'to_csv'但有一个错误 AttributeError: 'set' object has no attribute 'to_csv'

If data and df_row are Series for your solution need convert output to list and then to Series :如果datadf_row是您的解决方案的Series ,则需要将输出转换为list ,然后转换为Series

result = set(data).difference(set(df_row))
pd.Series(list(result)).to_csv("part1left.txt", index=False)

Or write set to file in pure python :或者在纯 python 中写入 set 到文件:

result = set(data).difference(set(df_row))
with open("part1left.txt", 'w') as file_handler:
    for item in result:
        file_handler.write("{}\n".format(item))

Pandas only solution with filtering by boolean indexing with Series.isin and inverting mask by ~ : Pandas 唯一的解决方案是通过使用Series.isinboolean indexing进行Series.isin并通过~反转掩码:

s = data[~data.isin(set(df_row))].drop_duplicates()
s.to_csv("part1left.txt", index=False)

EDIT:编辑:

If need create Series from files:如果需要从文件创建Series

import pandas as pd

temp=u"""12354564
25345754
23545454
11565654
46456456"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename1.csv'
data = pd.read_csv(pd.compat.StringIO(temp), squeeze=True, header=None, dtype=str)
print (data)
0    12354564
1    25345754
2    23545454
3    11565654
4    46456456
Name: 0, dtype: int64


temp=u"""23545454
11565654
46456456"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename2.csv'
df_row = pd.read_csv(pd.compat.StringIO(temp), squeeze=True, header=None, dtype=str)
print (df_row)
0    23545454
1    11565654
2    46456456
Name: 0, dtype: int64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我无法将输出数据帧转换为csv? AttributeError:'NoneType'对象没有属性'to_csv' - Why cant I get my output dataframe to convert to a csv? AttributeError: 'NoneType' object has no attribute 'to_csv' 'NoneType' 对象没有属性 'to_csv' - 'NoneType' object has no attribute 'to_csv' AttributeError:“ dict”对象没有属性“ to_csv” Google Analytics API报告V4 - AttributeError: 'dict' object has no attribute 'to_csv' Google Analytics API Reporting V4 AttributeError:“ CollectionReference”对象没有属性“ set” - AttributeError: 'CollectionReference' object has no attribute 'set' pandas.DataFrame.to_csv问题(“ tuple”对象没有属性“ to_csv”) - Issue with pandas.DataFrame.to_csv ('tuple' object has no attribute 'to_csv') AttributeError:“设置”对象没有属性“ b” - AttributeError: 'set' object has no attribute 'b' AttributeError: 'set' 对象没有属性 'timeout' - AttributeError: 'set' object has no attribute 'timeout' AttributeError: 'str' object 没有属性 'set' (Tkinter) - AttributeError: 'str' object has no attribute 'set' (Tkinter) “AttributeError:‘function’对象没有设置属性”tkinter - "AttributeError: 'function' object has no attribute set" tkinter AttributeError: '对象没有属性' - AttributeError: 'Object has no attribute'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM