我从CSV文件中加载了一些机器学习数据。前两列是观察结果,其余列是特征。

目前,我做以下事情:

data = pandas.read_csv('mydata.csv')

它会给出如下内容:

data = pandas.DataFrame(np.random.rand(10,5), columns = list('abcde'))

我想把这个数据帧切成两个数据帧:一个包含列a和b,一个包含列c, d和e。

不可能写出这样的东西

observations = data[:'c']
features = data['c':]

我不确定最好的方法是什么。我需要一个pd.Panel吗?

顺便说一下,我发现数据帧索引非常不一致:数据['a']是允许的,但数据[0]是不允许的。另一方面,数据['a':]是不允许的,但数据[0:]是允许的。 这有什么实际的原因吗?如果列以Int为索引,这真的很令人困惑,给定data[0] != data[0:1]


当前回答

你可以通过引用列表中每一列的名称来切片DataFrame的列,如下所示:

data = pandas.DataFrame(np.random.rand(10,5), columns = list('abcde'))
data_ab = data[list('ab')]
data_cde = data[list('cde')]

其他回答

你可以通过引用列表中每一列的名称来切片DataFrame的列,如下所示:

data = pandas.DataFrame(np.random.rand(10,5), columns = list('abcde'))
data_ab = data[list('ab')]
data_cde = data[list('cde')]

下面介绍如何使用不同的方法进行选择性列切片,包括基于选择标签的、基于索引的和基于选择范围的列切片。

In [37]: import pandas as pd    
In [38]: import numpy as np
In [43]: df = pd.DataFrame(np.random.rand(4,7), columns = list('abcdefg'))

In [44]: df
Out[44]: 
          a         b         c         d         e         f         g
0  0.409038  0.745497  0.890767  0.945890  0.014655  0.458070  0.786633
1  0.570642  0.181552  0.794599  0.036340  0.907011  0.655237  0.735268
2  0.568440  0.501638  0.186635  0.441445  0.703312  0.187447  0.604305
3  0.679125  0.642817  0.697628  0.391686  0.698381  0.936899  0.101806

In [45]: df.loc[:, ["a", "b", "c"]] ## label based selective column slicing 
Out[45]: 
          a         b         c
0  0.409038  0.745497  0.890767
1  0.570642  0.181552  0.794599
2  0.568440  0.501638  0.186635
3  0.679125  0.642817  0.697628

In [46]: df.loc[:, "a":"c"] ## label based column ranges slicing 
Out[46]: 
          a         b         c
0  0.409038  0.745497  0.890767
1  0.570642  0.181552  0.794599
2  0.568440  0.501638  0.186635
3  0.679125  0.642817  0.697628

In [47]: df.iloc[:, 0:3] ## index based column ranges slicing 
Out[47]: 
          a         b         c
0  0.409038  0.745497  0.890767
1  0.570642  0.181552  0.794599
2  0.568440  0.501638  0.186635
3  0.679125  0.642817  0.697628

### with 2 different column ranges, index based slicing: 
In [49]: df[df.columns[0:1].tolist() + df.columns[1:3].tolist()]
Out[49]: 
          a         b         c
0  0.409038  0.745497  0.890767
1  0.570642  0.181552  0.794599
2  0.568440  0.501638  0.186635
3  0.679125  0.642817  0.697628

注意:.ix自Pandas v0.20以来已弃用。您应该根据需要使用.loc或.iloc。

DataFrame。Ix index是你想要访问的。这有点令人困惑(我同意Pandas索引有时是令人困惑的!),但以下似乎是你想要的:

>>> df = DataFrame(np.random.rand(4,5), columns = list('abcde'))
>>> df.ix[:,'b':]
      b         c         d         e
0  0.418762  0.042369  0.869203  0.972314
1  0.991058  0.510228  0.594784  0.534366
2  0.407472  0.259811  0.396664  0.894202
3  0.726168  0.139531  0.324932  0.906575

其中.ix[行切片,列切片]是正在解释的内容。更多关于Pandas索引的信息,请访问:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-advanced

if数据帧是这样的:

group         name      count
fruit         apple     90
fruit         banana    150
fruit         orange    130
vegetable     broccoli  80
vegetable     kale      70
vegetable     lettuce   125

OUTPUT可以是

   group    name  count
0  fruit   apple     90
1  fruit  banana    150
2  fruit  orange    130

如果使用逻辑运算符np.logical_not

df[np.logical_not(df['group'] == 'vegetable')]

更多关于

https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.logic.html

其他逻辑运算符

logical_and(x1, x2, /[, out, where,…])计算的真值 x1和x2元素。 Logical_or (x1, x2, /[, out, where, casting, 计算x1或x2元素的真值。 logical_not(x, /[, out, where, casting,…])计算真值 NOT x元素的值。 logical_xor(x1, x2, /[, out, where, ..])按元素计算x1 XOR x2的真值。

其等效

 >>> print(df2.loc[140:160,['Relevance','Title']])
 >>> print(df2.ix[140:160,[3,7]])