在熊猫中另存为csv会向文件添加更多行

时间:2020-11-10 07:48:27

标签: python pandas csv

我遇到了奇怪的行为,我不知道该如何解决熊猫问题。 我已经处理了df。此数据库中的每一行都有唯一的ID,该ID是int号(作为对象类型)。我总共有121509行:

len(df)
>>>121509

len(df.id.unique().tolist())
>>>121509

问题:当我将其保存到csv并再次打开时,得到的行数超过121509,当我检查ID时,我看到它具有一些浮点数:

#save as csv
df.to_csv('results/final_table.csv')

#read it
tmpi=pd.read_csv(r'results/final_table.csv')

len(tmpi['id'l].unique().tolist())
>>>122287

#when I print the unique ID I find new ones that are float and werent in the original df like the last one here:
>>>[...
'22153793743',
 '22153806788',
 '22153810992',
 '0.06773762696917987'...]

我的问题是为什么会这样?为什么要添加行?我该如何接受它并保存我的数据库?

我的最终目标是使用正确的行数将此文件另存为excel

可能与之相关的其他信息:当我开始使用csv进行工作时,我不得不使用encoding ='cp1252',在保存但没有用时,我也曾尝试定义它

编辑:保存是Excel的工作。仍然不知道为什么会发生。

0 个答案:

没有答案