Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between .col and ['col'] in pandas

Tags:

pandas

I've been using pandas for a little while now and I've realised that I use

 df.col
 df['col'] 

interchangeably. Are they actually the same or am I missing something?

like image 476
draco_alpine Avatar asked Sep 07 '25 18:09

draco_alpine


1 Answers

Following on from the link in the comments.

df.col 

Simply refers to an attribute of the dataframe, similar to say

df.shape

Now if 'col' is a column name in the dataframe then accessing this attribute returns the column as series. This sometimes will be sufficient but

df['col']

will always work, and can also be used to add a new column to a dataframe.

like image 80
draco_alpine Avatar answered Sep 11 '25 23:09

draco_alpine