I have a data with 4 variables
>>>data
var1 var2 var3 var4
1 ee tree x
2 bb vfgr y
3 aa skfg z
4 bg ggff a
如何将var3和var4引入采样数据?
>>>sampledata = data(['var2','var3'])
错误:数据框不可调用
请帮助我如何提取我想要的变量?
预期产出:
>>>sampledata
var2 var3
ee tree
bb vfgr
aa skfg
bg ggff
答案 0 :(得分:1)
要选择列,需要将感兴趣的列列表传递给下标运算符:
In [104]:
sampledata = df[['var1','var3']]
sampledata
Out[104]:
var1 var3
0 1 tree
1 2 vfgr
2 3 skfg
3 4 ggff
您尝试做的是将dataframe对象视为一个引发错误的函数:
In [105]:
df()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-105-11abb07c39fc> in <module>()
----> 1 df()
TypeError: 'DataFrame' object is not callable
因此,在您的情况下,以下内容应该有效:
sampledata = data[['var2','var3']]