Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to access a column of a pandas dataframe

Tags:

python

pandas

For example I have a dataframe like this.

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

      Adj Close      Volume  
0  20435.240234  1531430000  

Using attribute or explicit naming both give me the same output:

sum(data.Date==data['Date']) == data.shape[0]

True

However I cannot access columns that are named with white space, like 'Adj Close' with df.columnname, but can do with df['columnname'].

Is using df['columnname'] strictly better than using df.columnname ?

like image 317
chrisckwong821 Avatar asked Sep 06 '17 02:09

chrisckwong821


People also ask

How do you access elements in a column of a DataFrame?

Accessing Single Column We can also access column of a DataFrame by using the below syntax, <DataFrame Object> [ <Column Name> ] <DataFrame Object> . <Column Name>

How do I get the value of a column from a DataFrame in Python?

get_value() function is used to quickly retrieve the single value in the data frame at the passed column and index. The input to the function is the row label and the column label.

How do you call a specific column in pandas?

To select a single column, use square brackets [] with the column name of the column of interest.

How do you access a column in a DataFrame using an index?

Use DataFrame. loc[] and DataFrame. iloc[] to select a single column or multiple columns from pandas DataFrame by column names/label or index position respectively. where loc[] is used with column labels/names and iloc[] is used with column index/position.


1 Answers

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.

like image 170
piRSquared Avatar answered Oct 13 '22 12:10

piRSquared