努力理解标题中的五个例子之间的区别。有些用例是用于系列框架还是数据框架?什么时候应该用一个而另一个呢?哪一个是等价物?
发布于 2018-05-12 01:51:31
df[x] -使用变量x索引列.返回pd.Seriesdf[[x]] -使用变量x索引/切片单列DataFrame。返回pd.DataFramedf['x'] -索引一个名为'x‘的列。返回pd.Seriesdf[['x']] -索引/切片单列DataFrame,只有一个列名为'x‘。返回pd.DataFramedf.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再读
发布于 2022-11-10 14:41:16
df‘标签’--单列df[‘标签’]--多个列,例如:如果‘标签’是自变量,'able‘是一个目标变量,那么在绘制它为df[’标签‘]和df'able’时。
https://stackoverflow.com/questions/50302180
复制相似问题