我有不同列中的数据,但我不知道如何提取它以将其保存在另一个变量中.
index a b c
1 2 3 4
2 3 4 5
Run Code Online (Sandbox Code Playgroud)
我该如何选择'a','b'并保存到DF1?
我试过了
df1 = df['a':'b']
df1 = df.ix[:, 'a':'b']
Run Code Online (Sandbox Code Playgroud)
似乎没有工作.
我只是试图通过整数访问命名的pandas列.
您可以使用按位置选择行df.ix[3].
但如何按整数选择列?
我的数据帧:
df=pandas.DataFrame({'a':np.random.rand(5), 'b':np.random.rand(5)})
Run Code Online (Sandbox Code Playgroud) 我一直在寻找通过python文档和论坛选择列的方法,但索引列上的每个示例都过于简单.
假设我有一个10 x 10的数据帧
df = DataFrame(randn(10, 10), index=range(0,10), columns=['A', 'B', 'C', 'D','E','F','G','H','I','J'])
Run Code Online (Sandbox Code Playgroud)
到目前为止,所有文档都只是一个索引的简单例子
subset = df.loc[:,'A':'C']
Run Code Online (Sandbox Code Playgroud)
要么
subset = df.loc[:,'C':]
Run Code Online (Sandbox Code Playgroud)
但是当我尝试索引多个非顺序列时,我得到一个错误,就像这样
subset = df.loc[:,('A':'C', 'E')]
Run Code Online (Sandbox Code Playgroud)
如果我想从A到C,E和G中选择A列,我将如何在Pandas中编入索引?看来这个逻辑不起作用
subset = df.loc[:,('A':'C', 'E', 'G':'I')]
Run Code Online (Sandbox Code Playgroud)
我觉得解决方案非常简单,但我无法解决这个错误.谢谢!
我试图在数据框中提取第7到第14列.然而
df[0:3] 只给出第1到第3行.
如果我想查找列,有谁知道我该怎么办?
我知道如何使用列名这样做,df['a']但由于名称太多,我只想输入类似于df[,7:14]R的内容.
提前致谢.
我一直在尝试从数据集中为所有行选择一组特定的列。我尝试了以下类似的方法。
train_features = train_df.loc[,[0,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]]
Run Code Online (Sandbox Code Playgroud)
我想提一下,所有行都包含在内,但只需要编号的列即可。有没有更好的方法来解决这个问题。
样本数据:
age job marital education default housing loan equities contact duration campaign pdays previous poutcome emp.var.rate cons.price.idx cons.conf.idx euribor3m nr.employed y
56 housemaid married basic.4y 1 1 1 1 0 261 1 999 0 2 1.1 93.994 -36.4 3.299552287 5191 1
37 services married high.school 1 0 1 1 0 226 1 999 0 2 1.1 93.994 -36.4 0.743751247 5191 1
56 services married high.school 1 1 0 1 0 307 1 999 0 2 …Run Code Online (Sandbox Code Playgroud)