[Python Dataframe] What is the difference between df.column and df['column']?

What is the difference between the below two and are there certain scenarios where you would use one over the other?

df[‘Lowercase Name’] = df.Name.apply(str.lower)
df[‘Lowercase Name’] = df[‘Name’].apply(str.lower)

https://www.codecademy.com/courses/data-processing-pandas/lessons/pandas-ii/exercises/columns-apply

Hey Michelle-law,

Both examples do the same thing. However, depending if the Column name has spaces then you should use the latter example always (e.g., . df["Column Name with Space"]). For example:

import pandas as pd

data = {
  "Column1": [420, 380, 390],
  "Column Two": [50, 40, 45]
}


df = pd.DataFrame(data)

print(df.Column1)  
print(df['Column Two'])  

You can access Column1 via .dot notation because it has no spaces. However, if you tried to access Column Two using the .dot notation, you will get an SyntaxError. Therefore you must access it using the dictionary[key] method.

Does that makes sense? Let me know if that answers your questions!

2 Likes