pandas 返回未命名的列 [英] pandas returning the unnamed columns

查看:56
本文介绍了 pandas 返回未命名的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我在excel表中的数据示例.

The following is example of data I have in excel sheet.

A    B   C 
1    2   3 
4    5   6

我正在尝试使用以下代码获取列名称:

I am trying to get the columns name using the following code:

p1 = list(df1t.columns.values)

输出是这样的

[A, B, C, 'Unnamed: 3', 'unnamed 4', 'unnamed 5', .....] 

我查了excel表,只有A、B、C三栏,其他栏都是空的.有什么建议吗?

I checked the excel sheet, there is only three columns named A, B, and C. Other columns are blank. Any suggestion?

推荐答案

出现问题,有些单元格不为空,但包含一些空格.

There is problem some cells are not empty but contains some whitespaces.

如果需要过滤Unnamed的列名:

cols = [col for col in df if not col.startswith('Unnamed:')]
print (cols)
['A', 'B', 'C']

带有文件的示例:

df = pd.read_excel('https://dl.dropboxusercontent.com/u/84444599/file_unnamed_cols.xlsx')
print (df)
     A    B    C Unnamed: 3 Unnamed: 4 Unnamed: 5 Unnamed: 6 Unnamed: 7
0  4.0  6.0  8.0        NaN        NaN        NaN        NaN        NaN
1  NaN  NaN  NaN                   NaN        NaN        NaN        NaN
2  NaN  NaN  NaN        NaN                   NaN        NaN           
3  NaN  NaN  NaN        NaN        NaN                              NaN

cols = [col for col in df if not col.startswith('Unnamed:')]
print (cols)
['A', 'B', 'C']

另一种解决方案:

cols = df.columns[~df.columns.str.startswith('Unnamed:')]
print (cols)
Index(['A', 'B', 'C'], dtype='object')

并通过 cols 返回所有列:

And for return all columns by cols use:

print (df[cols])
     A    B    C
0  4.0  6.0  8.0
1  NaN  NaN  NaN
2  NaN  NaN  NaN
3  NaN  NaN  NaN

如有必要,删除所有 NaN 的行:

And if necessary remove all NaNs rows:

print (df[cols].dropna(how='all'))
     A    B    C
0  4.0  6.0  8.0

这篇关于 pandas 返回未命名的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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