在 pandas 中,df ['column']和df.column有什么区别? [英] In pandas, what's the difference between df['column'] and df.column?
问题描述
我正在通过熊猫进行数据分析和学习大量知识.但是,一件事不断出现.本书通常将数据框的列称为df['column']
,但是有时无需解释,本书就使用df.column
.
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.列:
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
推荐答案
要设置值,您需要使用df['column'] = series
.
for setting, values, you need to use df['column'] = series
.
一旦完成此操作,以后就可以使用df.column
引用该列,并假定它是有效的python名称. (因此df.column
可行,但是df.6column
仍必须通过df['6column']
访问)
once this is done however, you can refer to that column in the future with df.column
, assuming it's a valid python name. (so df.column
works, but df.6column
would still have to be accessed with df['6column']
)
我认为这里的细微差别是,当您使用df['column'] = ser
设置某些内容时,pandas会继续执行并将其添加到列中/还做其他一些事情(我相信通过覆盖__setitem__
中的功能.如果您df.column = ser
,就像向使用__setattr__
的任何现有对象添加新字段一样,熊猫似乎并没有覆盖此行为.
i think the subtle difference here is that when you set something with df['column'] = ser
, pandas goes ahead and adds it to the columns/does some other stuff (i believe by overriding the functionality in __setitem__
. if you do df.column = ser
, it's just like adding a new field to any existing object which uses __setattr__
, and pandas does not seem to override this behavior.
这篇关于在 pandas 中,df ['column']和df.column有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!