[英]How to write tweet IDs to a CSV file as strings
I have a column which contains a list of tweet IDs. 我有一列包含推特ID的列表。 A tweet ID could get as long as this '475685437424427008'.
推特ID的长度可以与此“ 475685437424427008008”一样长。 When I write this to a CSV file using the data.to_csv() method they get written as float.
当我使用data.to_csv()方法将其写入CSV文件时,它们将被写为float。 How do I retain the numbers as string?
如何将数字保留为字符串?
Even after writing a list of strings (long int converted to string) to a csv file using dataframe, while reading the same file pandas reads the column as int 64. While reading a csv file into dataframe we can specify the format for particular column. 即使在使用数据帧将字符串列表(将long int转换为字符串)写入csv文件之后,在读取同一文件时,pandas也会将该列读取为int64。在将csv文件读取到dataframe中时,我们可以指定特定列的格式。
sample.csv file contains: sample.csv文件包含:
45646879879779
54121798454644
79841321321549
44654646449879
I'm running: 我在跑:
import pandas as pd
df = pd.read_csv('sample.csv', converters={'ID': str})
Where: 哪里:
The dtype
is probably a float but displayed as an integer, change the type: dtype
可能是浮点型,但显示为整数,请更改类型:
df['tweet_id'] = df['tweet_id'].astype(str)
You can confirm if this worked or not, using df.dtypes
您可以使用
df.dtypes
确认此方法df.dtypes
It should display: 它应该显示:
In [5]:
df['a'] = df['a'].astype(str)
df.dtypes
Out[5]:
a object
dtype: object
should to the trick when you call to_csv
调用
to_csv
时应该to_csv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.