Proper way to access a column of a pandas dataframe

前端 未结 1 1374
没有蜡笔的小新
没有蜡笔的小新 2021-01-18 17:20

For example I have a dataframe like this.

     Date          Open          High           Low         Close  \\
0  2009-08-25  20246.789063  20476.250000  20         


        
相关标签:
1条回答
  • 2021-01-18 17:48

    Using . as a column accessor is a convenience. There are many limitations beyond having spaces in the name. For example, if your column is named the same as an existing dataframe attribute or method, you won't be able to use it with a .. A non-exhaustive list is mean, sum, index, values, to_dict, etc. You also cannot reference columns with numeric headers via the . accessor.

    So, yes, ['col'] is strictly better than .col because it is more consistent and reliable.

    0 讨论(0)
提交回复
热议问题