获取带有空格的 pandas df 列的 len()

getting len() of the pandas df column with a whitespace

我有以下 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. len(df['Site In'])len(df['Site Out'])
  2. 您可以通过分配 df['Site Out'].column.valuesdf['Site In'].column.values 来重命名列,消除空格,您的方法会起作用。
  3. 或者,您可以使用 df.iloc(1) for df['Site In'] 或 df.iloc(2) for df['Site Out'] 来解决它们。

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