In a Pandas DataFrame, what is the difference between using square brackets or dots to “copy a column”?

i.e:.

import pandas d = {'col1': 2, 'col2': 2.5} df = pandas.DataFrame(data=d, index=[0]) print(df['col2']) print(df.col2) 

The conclusion is the same.

Does this answer respond to this case?

What is the difference between a square bracket and dot notes in Python?

+8
python pandas dataframe
source share
2 answers

"Point notation", i.e. df.col2 , is access to the attribute , which is displayed as a convenience.

You can access the index in the Series column, in the DataFrame and in the panel element directly as an attribute:

df['col2'] does the same: it returns the pd.Series column.

A few caveats about accessing an attribute:

  • you cannot add a column ( df.new_col = x will not work, worse: it will silently actually create a new attribute, not a column - think about it here.)
  • it will not work if you have spaces in the column name or if the column name is an integer.
+9
source share

They are the same as if you were referring to the same column with a simple name, but you can do more with a musical bracket. You can only use df.col if the column name is a valid Python identifier (for example, it does not contain spaces or other similar materials). In addition, you may encounter surprises if your column name collides with the pandas method name (e.g. sum ). In parentheses, you can select multiple columns (for example, df[['col1', 'col2']] ) or add a new column ( df['newcol'] = ... ) that cannot be done using point access.

The other question you are referring to applies, but this is a much more general question. Python objects determine how operators are applied to them . and [] . pandas DataFrames decided to make them the same for this limited case of accessing individual columns with the caveats described above.

+8
source share

All Articles