3

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?

draco_alpine
  • 769
  • 11
  • 25

3 Answers3

5

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.

draco_alpine
  • 769
  • 11
  • 25
3

I think this is kind of obvious.....

You cannot use df.col if the column name 'col' has a space in it. But df['col'] always works. e.g,

df['my col'] works but df.my col will not work.

0

I'll note there's a difference in how some methods consume data. For example, in the LifeTimes library if I use dataframe.col with some methods, the method will consider the column to be an ndarray and throw an exception that the data must be 1-dimensional.

If however I use dataframe['col'] then the method will consume the data as expected.

MisterJT
  • 412
  • 4
  • 15