What is the difference between using squared brackets or dot to access a column?

前端 未结 3 878
感情败类
感情败类 2020-11-22 06:55

In both the bellow cases:

import pandas

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

print(df[\'col2\'])
print(df.col2)
         


        
3条回答
  •  一个人的身影
    2020-11-22 07:14

    They are the same as long you're accessing a single column with a simple name, but you can do more with the bracket notation. You can only use df.col if the column name is a valid Python identifier (e.g., does not contains spaces and other such stuff). Also, you may encounter surprises if your column name clashes with a pandas method name (like sum). With brackets you can select multiple columns (e.g., df[['col1', 'col2']]) or add a new column (df['newcol'] = ...), which can't be done with dot access.

    The other question you linked to applies, but that is a much more general question. Python objects get to define how the . and [] operators apply to them. Pandas DataFrames have chosen to make them the same for this limited case of accessing single columns, with the caveats described above.

提交回复
热议问题