0

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
oakca
  • 1,408
  • 1
  • 18
  • 40
  • 2
    `df['Site In']` – yatu Feb 27 '19 at 15:15
  • ofc this would work :) but I am looking for something like the example I gave with Transmission – oakca Feb 27 '19 at 15:16
  • 1
    Stop accessing columns as attributes, the fact this doesn't work plus other reasons should tell you to stop – EdChum Feb 27 '19 at 15:17
  • There is **no** point in taking the length of a column. It's guarnateed to be the same as `len(df)`. If perhaps you intend for some different calculation you should express that. – ALollz Feb 27 '19 at 15:21
  • @ALollz with unique() it wont be same with len(df) – oakca Feb 27 '19 at 15:22
  • Related: https://stackoverflow.com/questions/30250282/whats-the-difference-between-the-square-bracket-and-dot-notations-in-python – Nick is tired Feb 27 '19 at 15:23
  • 1
    if tat is so important `df=df.rename(columns={'Site In':'Site_In'})` then do `len(df.Site_In)` – anky Feb 27 '19 at 15:23
  • @ALollz question simply was to learn if there is a way to access the columns of the df like an attribute. It works with non whitespace containing column names. It does not work with whitespace containing column names. len was just an example. So I just asked if there is a way to do that. or since it has whitespace it is impossible? I surely know how to get the length with a braclet as yatu also said... – oakca Feb 27 '19 at 15:26
  • @EdChum I am not seing any bad application of accesing like attributes. Just this special case... Can u clerify more why not to do this? – oakca Feb 27 '19 at 15:28
  • If you happen to have a column named for instance `'mean'` then calling `df.mean` calls the function not try to access the column, also the behaviour is not what you expect when you try to add a column by using `df.foo = some_val` this may add an attribute rather than a column, you should get into the habit of never accessing columns like this – EdChum Feb 27 '19 at 15:29
  • @EdChum okay this is more explaining, so why could u still access the columns like attributes? what is the reason for this? – oakca Feb 27 '19 at 15:31
  • it was a convenience from the beginning, only afterwards did people find cases where it doesn't behave the way people think, it should be avoided – EdChum Feb 27 '19 at 15:33

1 Answers1

3
  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.

hd1
  • 33,938
  • 5
  • 80
  • 91