pandas :如何摆脱数据框中的“未命名:”列 [英] Pandas: how to get rid of `Unnamed:` column in a dataframe

查看:240
本文介绍了 pandas :如何摆脱数据框中的“未命名:”列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,有时当我从 df 读取 csv 时,我会得到一个不需要的类索引列名为未命名:0 。这很烦人!我试过了

I have a situation wherein sometimes when I read a csv from df I get an unwanted index-like column named unnamed:0. This is very annoying! I have tried

merge.to_csv('xy.df', mode = 'w', inplace=False)

我认为这是一个解决方案,但我仍然得到未命名:0 专栏!有没有人对此有所了解?

which I thought was a solution to this, but I am still getting the unnamed:0 column! Does anyone have an idea on this?

推荐答案

这是索引列,传递 index = False 不写出来,请参阅 docs

It's the index column, pass index=False to not write it out, see the docs

示例:

In [37]:
df = pd.DataFrame(np.random.randn(5,3), columns=list('abc'))
pd.read_csv(io.StringIO(df.to_csv()))

Out[37]:
   Unnamed: 0         a         b         c
0           0  0.109066 -1.112704 -0.545209
1           1  0.447114  1.525341  0.317252
2           2  0.507495  0.137863  0.886283
3           3  1.452867  1.888363  1.168101
4           4  0.901371 -0.704805  0.088335

与以下项比较:

In [38]:
pd.read_csv(io.StringIO(df.to_csv(index=False)))

Out[38]:
          a         b         c
0  0.109066 -1.112704 -0.545209
1  0.447114  1.525341  0.317252
2  0.507495  0.137863  0.886283
3  1.452867  1.888363  1.168101
4  0.901371 -0.704805  0.088335

你也可以选择通过传递 index_col =告诉 read_csv 第一列是索引列0

You could also optionally tell read_csv that the first column is the index column by passing index_col=0:

In [40]:
pd.read_csv(io.StringIO(df.to_csv()), index_col=0)

Out[40]:
          a         b         c
0  0.109066 -1.112704 -0.545209
1  0.447114  1.525341  0.317252
2  0.507495  0.137863  0.886283
3  1.452867  1.888363  1.168101
4  0.901371 -0.704805  0.088335

这篇关于 pandas :如何摆脱数据框中的“未命名:”列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆