繁体   English   中英

pandas python中没有列名

[英]No Column Names in pandas python

关于熊猫数据框的基本问题。 我有一个带数据点的1x1数据帧,并且没有列标题(当前)。 df[0,0]不起作用,因为我认为它期待列名。 另外, df.0不起作用, df[0,'']也不起作用。 df.ix[0,0]确实有效。

一般来说,我是否需要有一个列名? 将列名与pandas数据帧一起使用是最佳做法吗? 如果我的sql查询没有列标题,那么最好在那时添加它们吗?

谢谢您的帮助。

不,您不需要分配列名,也不需要它们来访问任何元素。

In [12]: df = pd.DataFrame([0])

In [13]: df.ix[0,0]
Out[13]: 0

In [14]: df[0][0]
Out[14]: 0

实际上,您可以想到已经有名称的列 - 它是整数0.看看提供名称时会发生什么

In [15]: df    #Before naming the column
Out[15]:
   0
0  0

In [16]: df.columns = ['ColA']
In [17]: df    #Renamed
Out[17]:
   ColA
0     0

In [18]: df['ColA'][0]    #Now you can access the column using the new name
Out[18]: 0

In [19]: df[0][0]         #... but trying the old name will not work
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)

KeyError: 'no item named 0'

您仍然可以像以前一样使用DataFrame.ix

In [20]: df.ix[0,0]
Out[20]: 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM