用空格获取pandas df列的len()

时间:2019-02-27 15:14:30

标签: python pandas content-length

我有以下df:

df
   Site In Site Out Transmission Commodity     parameter         value unit
0      Mid    North         hvac      Elec           eff  9.000000e-01     
1      Mid    North         hvac      Elec      inv-cost  1.650000e+06     
2      Mid    North         hvac      Elec      fix-cost  0.000000e+00     
3      Mid    North         hvac      Elec      var-cost  0.000000e+00     
4      Mid    North         hvac      Elec      inst-cap  0.000000e+00     
5      Mid    North         hvac      Elec        cap-lo  0.000000e+00     
6      Mid    North         hvac      Elec        cap-up  1.500000e+15     
7      Mid    North         hvac      Elec          wacc  7.000000e-02     
8      Mid    North         hvac      Elec  depreciation  4.000000e+01     
9      Mid    South         hvac      Elec           eff  9.000000e-01
...     

当我遵循它时,它会起作用:

len(df.Transmission)
54

我如何获得'Site In''Site Out'的len(),因为它们的名称中包含空格,所以我找不到使用.column_name的方法? ??

说实话,有几种获取长度的方法,但是在这种情况下有没有使用.column_name的方法?

以下操作无效:

len(df.Site In)
*** SyntaxError: invalid syntax
len(df.SiteIn)
*** AttributeError: 'DataFrame' object has no attribute 'SiteIn'
len(df.Site_In)
*** AttributeError: 'DataFrame' object has no attribute 'Site_In'
len(df.Site' 'In)
*** SyntaxError: invalid syntax

1 个答案:

答案 0 :(得分:3)

  1. len(df['Site In'])len(df['Site Out'])
  2. 您可以通过分配df['Site Out'].column.valuesdf['Site In'].column.values消除空格来重命名列,并且您的方法可以使用。
  3. 您也可以使用df ['Site In']的df.iloc(1)或df ['Site Out']的df.iloc(2)来解决它们。

也有很多方法,但是这些是最常见的。

相关问题