如何在转换为数据帧时删除csv中的自动索引

2024-04-23 14:58:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用pandas将我的csv转换为数据帧,但当打印它时,它会自动打印索引,我尝试使用它

index=false

dataframe.drop(['Unnamed 0']

但不工作 这是密码

data = pd.read_csv('testlast.csv', sep='\t', index=False)
print(data)

输出看起来像

enter image description here

这就是csventer image description here中的句子的样子

如何删除自动生成的数字


Tags: csv数据false密码dataframepandasreaddata
1条回答
网友
1楼 · 发布于 2024-04-23 14:58:18

大熊猫中的SeriesDataFrame一直是索引,原因是docs

The axis labeling information in pandas objects serves many purposes:

Identifies data (i.e. provides metadata) using known indicators, important for analysis, visualization, and interactive console display.
Enables automatic and explicit data alignment.
Allows intuitive getting and setting of subsets of the data set.

因此,对于处理数据,您可以忽略它(如果数据处理不需要):

df1 = (df['sentences'].str.split(expand=True)
                      .stack()
                      .value_counts()
                      .rename_axis('a')
                      .reset_index(name='b'))

避免将索引写入文件的最后一步是将index=False参数添加到^{}

df1.to_csv(file, index=False)

相关问题 更多 >