Search code examples
pythonpandascontent-length

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


I have the following 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
...     

when I do following it works:

len(df.Transmission)
54

How would I get the len() of the 'Site In' or 'Site Out', since they got whitespace in their name, I could not find a way to use .column_name ???

To be honest there are several ways to get the length but is there a way to use .column_name in this case?

Following does not work:

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

Solution

    1. len(df['Site In']) and len(df['Site Out'])
    2. You could rename the columns by assigning df['Site Out'].column.values and df['Site In'].column.values eliminating the spaces and your method would work.
    3. You could, alternatively, address them using df.iloc(1) for df['Site In'] or df.iloc(2) for df['Site Out'].

    There are a number of the ways as well, but these are the most common.