df [x],df [[x]],df ['x'],df [['x']]和df.x之间的差异 [英] Difference between df[x], df[[x]], df['x'] , df[['x']] and df.x

查看:99
本文介绍了df [x],df [[x]],df ['x'],df [['x']]和df.x之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

努力理解标题中5个示例之间的区别.系列和数据框架有一些用例吗?什么时候应该使用另一个?哪个等价?

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. df[x] —使用变量x索引一列.返回pd.Series
  2. df[[x]] —使用变量x索引/切片单列DataFrame.返回pd.DataFrame
  3. df['x'] —索引名为"x"的列.返回pd.Series
  4. df[['x']] —索引/切片仅具有一个名为"x"的列的单列DataFrame.返回pd.DataFrame
  5. df.x —点访问符表示法,等同于df['x'](但是,有
  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

使用单括号[...],您只能将单个列索引为系列.使用双括号[[...]],您可以根据需要选择任意多的列,并且这些列将作为新DataFrame的一部分返回.

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.

设置

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


进一步阅读
建立索引并选择数据


Further reading
Indexing and Selecting Data

这篇关于df [x],df [[x]],df ['x'],df [['x']]和df.x之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆