我从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]


当前回答

让我们以来自海运包的titanic数据集为例

# Load dataset (pip install seaborn)
>> import seaborn.apionly as sns
>> titanic = sns.load_dataset('titanic')

使用列名

>> titanic.loc[:,['sex','age','fare']]

使用列索引

>> titanic.iloc[:,[2,3,6]]

使用ix(比Pandas更老<。20版本)

>> titanic.ix[:,[‘sex’,’age’,’fare’]]

or

>> titanic.ix[:,[2,3,6]]

使用重索引方法

>> titanic.reindex(columns=['sex','age','fare'])

其他回答

同样,给定一个数据帧

数据

就像在你的例子中,如果你只想提取列a和d(即第1列和第4列),那么熊猫数据框架中的iloc方法就是你所需要的,并且可以非常有效地使用。您所需要知道的只是想要提取的列的索引。例如:

>>> data.iloc[:,[0,3]]

会给你

          a         d
0  0.883283  0.100975
1  0.614313  0.221731
2  0.438963  0.224361
3  0.466078  0.703347
4  0.955285  0.114033
5  0.268443  0.416996
6  0.613241  0.327548
7  0.370784  0.359159
8  0.692708  0.659410
9  0.806624  0.875476

2017答案- pandas 0.20: .ix已弃用。使用.loc

请参阅文档中的弃用部分

.loc使用基于标签的索引来选择行和列。标签是索引或列的值。使用.loc进行切片包括最后一个元素。

让我们假设我们有一个包含以下列的DataFrame: Foo, bar, quz, ant, cat, sit, dat。

# selects all rows and all columns beginning at 'foo' up to and including 'sat'
df.loc[:, 'foo':'sat']
# foo bar quz ant cat sat

.loc接受与Python列表对行和列所做的相同的切片符号。切片符号为start:stop:step

# slice from 'foo' to 'cat' by every 2nd column
df.loc[:, 'foo':'cat':2]
# foo quz cat

# slice from the beginning to 'bar'
df.loc[:, :'bar']
# foo bar

# slice from 'quz' to the end by 3
df.loc[:, 'quz'::3]
# quz sat

# attempt from 'sat' to 'bar'
df.loc[:, 'sat':'bar']
# no columns returned

# slice from 'sat' to 'bar'
df.loc[:, 'sat':'bar':-1]
sat cat ant quz bar

# slice notation is syntatic sugar for the slice function
# slice from 'quz' to the end by 2 with slice function
df.loc[:, slice('quz',None, 2)]
# quz cat dat

# select specific columns with a list
# select columns foo, bar and dat
df.loc[:, ['foo','bar','dat']]
# foo bar dat

您可以按行和列进行切片。例如,如果你有5行,标签是v w x y z

# slice from 'w' to 'y' and 'foo' to 'ant' by 3
df.loc['w':'y', 'foo':'ant':3]
#    foo ant
# w
# x
# y

您可以使用截断方法

df = pd.DataFrame(np.random.rand(10, 5), columns = list('abcde'))

df_ab = df.truncate(before='a', after='b', axis=1)
df_cde = df.truncate(before='c', axis=1)

注意:.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的真值。