I'm working my way through Pandas for Data Analysis and learning a ton. However, one thing keeps coming up. The book typically refers to columns of a dataframe as df['column']
however, sometimes without explanation the book uses df.column
.
I don't understand the difference between the two. Any help would be appreciated.
Below is come code demonstrating the what I am talking about:
In [5]:
import pandas as pd
data = {'column1': ['a', 'a', 'a', 'b', 'c'],
'column2': [1, 4, 2, 5, 3]}
df = pd.DataFrame(data, columns = ['column1', 'column2'])
df
Out[5]:
column1 column2
0 a 1
1 a 4
2 a 2
3 b 5
4 c 3
5 rows × 2 columns
df.column:
In [8]:
df.column1
Out[8]:
0 a
1 a
2 a
3 b
4 c
Name: column1, dtype: object
df['column']:
In [9]:
df['column1']
Out[9]:
0 a
1 a
2 a
3 b
4 c
Name: column1, dtype: object
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…