努力理解标题中5个示例之间的区别。系列和数据框架有一些用例吗?什么时候应该使用另一个?哪个等效?
df[x]
-使用变量索引列x
。退货pd.Series
df[[x]]
—使用variable对单列DataFrame进行索引/切片x
。退货pd.DataFrame
df['x']
—索引名为“ x”的列。退货pd.Series
df[['x']]
—索引/切片仅具有一个名为“ x”的列的单列DataFrame。退货pd.DataFrame
df.x
-点访问符号,相当于df['x']
(有,但是,限制什么x
要成功地使用,如果点符号是可以被命名)。退货pd.Series
使用单括号,[...]
您只能将单个列索引为系列。使用中括号,[[...]]
您可以根据需要选择任意多的列,这些列作为新DataFrame的一部分返回。
设定
df
ID x
0 0 0
1 1 15
2 2 0
3 3 0
4 4 0
5 5 15
x = 'ID'
例子
df[x]
0 0
1 1
2 2
3 3
4 4
5 5
Name: ID, dtype: int64
type(df[x])
pandas.core.series.Series
df['x']
0 0
1 15
2 0
3 0
4 0
5 15
Name: x, dtype: int64
type(df['x'])
pandas.core.series.Series
df[[x]]
ID
0 0
1 1
2 2
3 3
4 4
5 5
type(df[[x]])
pandas.core.frame.DataFrame
df[['x']]
x
0 0
1 15
2 0
3 0
4 0
5 15
type(df[['x']])
pandas.core.frame.DataFrame
df.x
0 0
1 15
2 0
3 0
4 0
5 15
Name: x, dtype: int64
type(df.x)
pandas.core.series.Series
进一步阅读建立
索引和选择数据
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句