Does "df['var'].map(df2)" and "df.var.map(df2)" always produce the same result?

Viewed 37

I have a dataframe df with a column var, and another dataframe df2 with columns var and var2. Both columns var in 2 dataframes are exactly the same.

In my example, df['var'].map(df2) and df.var.map(df2) yield the same result. I would like to ask if this is just a coincidence in my particular dataset, or it always holds.

Thank you so much!

Update: In my example, these below codes also produce the same result.

df.groupby('parent_id')['parent_id'].transform('count').tolist()

and

df.groupby('parent_id').parent_id.transform('count').tolist()

This gives me a feeling that df.groupby('parent_id')['parent_id'] and df.groupby('parent_id').parent_id produce the same result.

1 Answers

Yes (as long as the column exists in your data). It's syntactic sugar called attribute access. See the pandas documentation here.

Related