Difference between df[x], df[[x]], df['x'] , df[['x']] and df.x

微笑、不失礼 提交于 2020-01-30 04:41:15

问题


Struggling to understand the difference between the 5 examples in the title. Are some use cases for series vs. data frames? When should one be used over the other? Which are equivalent?


回答1:


  1. df[x] — index a column using variable x. Returns pd.Series
  2. df[[x]] — index/slice a single-column DataFrame using variable x. Returns pd.DataFrame
  3. df['x'] — index a column named 'x'. Returns pd.Series
  4. df[['x']] — index/slice a single-column DataFrame having only one column named 'x'. Returns pd.DataFrame
  5. df.x — dot accessor notation, equivalent to df['x'] (there are, however, limitations on what x can be named if dot notation is to be successfully used). Returns pd.Series

With single brackets [...] you may only index a single column out as a Series. With double brackets, [[...]], you may select as many columns as you need, and these columns are returned as part of a new DataFrame.


Setup

df
   ID   x
0   0   0
1   1  15
2   2   0
3   3   0
4   4   0
5   5  15

x = 'ID'

Examples

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

Further reading
Indexing and Selecting Data



来源:https://stackoverflow.com/questions/50302180/difference-between-dfx-dfx-dfx-dfx-and-df-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!