如何利用数据帧的大熊猫柱片如何利用数据帧的大熊猫柱片(How to take column-slic

2019-05-14 09:22发布

我从一个CSV文件中加载一些机器学习数据。 前2列是观察和剩余的列是特征。

目前,我做到以下几点:

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

这给类似:

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

我想以两种dataframes切片此数据帧:一种含列ab和含有列一个cde

这是不可能写类似

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

我不知道最好的方法是什么。 我需要一个pd.Panel

顺便说一句,我觉得数据帧索引很不一致: data['a']是允许的,但data[0]不是。 在另一侧, data['a':]不允许但data[0:]是。 是否有此实际原因是什么? ,这实在是混乱,如果列被诠释索引因为data[0] != data[0:1]

Answer 1:

2017年回答 - 大熊猫0.20:.IX已被弃用。 使用的.loc

请参阅在文档中弃用

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

假设我们有以下的列的数据帧:
foobarquzantcatsatdat

# 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列标签vwxyz

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


Answer 2:

该DataFrame.ix指数是你想成为什么样访问。 这是一个有点混乱(我同意,熊猫索引在时间傻了眼!),但以下似乎做你想要什么:

>>> 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 [行切片,切片栏]为被解释什么。 更多关于熊猫索引位置: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-advanced

注: .ix因为熊猫v0.20已被弃用。 您应该使用.loc.iloc适当。



Answer 3:

让使用钛酸数据集从seaborn包作为一个例子

# 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]]

使用1×(已成熊猫<0.20版本)

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

要么

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

使用重新索引方法

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


Answer 4:

此外,给定一个数据帧

数据

在你的例子,如果你想提取列,只有D(EI第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


Answer 5:

你可以沿的列切片DataFrame参考每一列的名称列表中,像这样:

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


Answer 6:

如果你来到这里寻找切片列的两个范围,并将它们结合在一起(比如我),你可以这样做

op = df[list(df.columns[0:899]) + list(df.columns[3593:])]
print op

这将创建第一900列(全部)列> 3593新的数据框(假设你有你的数据集大约4000列)。



Answer 7:

这里是你如何可以使用不同的方法做有选择性的列切片, 包括基于选择性标签,基于索引和基于列的切片选择的范围。

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


Answer 8:

它的等效

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


Answer 9:

如果数据帧的样子说:

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

其他逻辑运算符

  1. logical_and(X1,X2,/ [,出,其中,...])计算X1和X2逐元素的真值。

  2. 逻辑或(X1,X2,/ [,出,其中,铸塑,...])计算X1或X2逐元素的真值。

  3. logical_not(X,/ [,出,铸造,其中,,...])计算的X不逐元素的真值。
  4. logical_xor(X1,X2,/ [,出,其中,..])计算X1 XOR X2,逐元素的真值。


文章来源: How to take column-slices of dataframe in pandas