如何将列和行的 Pandas DataFrame 子集转换为 numpy 数组? [英] How to convert a pandas DataFrame subset of columns AND rows into a numpy array?
问题描述
我想知道是否有一种更简单、内存高效的方法来从 Pandas DataFrame 中选择行和列的子集.
例如,给定这个数据框:
<前>df = DataFrame(np.random.rand(4,5), columns = list('abcde'))打印文件a b c d0 0.945686 0.000710 0.909158 0.892892 0.3266701 0.919359 0.667057 0.462478 0.008204 0.4730962 0.976163 0.621712 0.208423 0.980471 0.0483343 0.459039 0.788318 0.309892 0.100539 0.753992我只需要列 'c' 的值大于 0.5 的那些行,但我只需要这些行的列 'b' 和 'e'.
这是我想出的方法 - 也许有更好的熊猫"方法?
<前>locs = [df.columns.get_loc(_) for _ in ['a', 'd']]打印 df[df.c > 0.5][locs]广告0 0.945686 0.892892我的最终目标是将结果转换为 numpy 数组以传递给 sklearn 回归算法,因此我将使用上面的代码:
<前>训练集 = 数组(df[df.c > 0.5][locs])... 这让我很恼火,因为我最终在内存中得到了一个巨大的数组副本.也许还有更好的方法?
.loc
同时接受行和列选择器(正如 .ix/.iloc
仅供参考)这也是一次性完成的.
在 [1]: df = DataFrame(np.random.rand(4,5), columns = list('abcde'))在 [2]: df出[2]:a b c d0 0.669701 0.780497 0.955690 0.451573 0.2321941 0.952762 0.585579 0.890801 0.643251 0.5562202 0.900713 0.790938 0.952628 0.505775 0.5823653 0.994205 0.330560 0.286694 0.125061 0.575153在 [5] 中:df.loc[df['c']>0.5,['a','d']]出[5]:广告0 0.669701 0.4515731 0.952762 0.6432512 0.900713 0.505775
如果你想要这些值(尽管这应该直接传递给 sklearn);框架支持数组接口
在[6]中:df.loc[df['c']>0.5,['a','d']].values出[6]:数组([[ 0.66970138, 0.45157274],[ 0.95276167, 0.64325143],[ 0.90071271, 0.50577509]])
I'm wondering if there is a simpler, memory efficient way to select a subset of rows and columns from a pandas DataFrame.
For instance, given this dataframe:
df = DataFrame(np.random.rand(4,5), columns = list('abcde')) print df a b c d e 0 0.945686 0.000710 0.909158 0.892892 0.326670 1 0.919359 0.667057 0.462478 0.008204 0.473096 2 0.976163 0.621712 0.208423 0.980471 0.048334 3 0.459039 0.788318 0.309892 0.100539 0.753992
I want only those rows in which the value for column 'c' is greater than 0.5, but I only need columns 'b' and 'e' for those rows.
This is the method that I've come up with - perhaps there is a better "pandas" way?
locs = [df.columns.get_loc(_) for _ in ['a', 'd']] print df[df.c > 0.5][locs] a d 0 0.945686 0.892892
My final goal is to convert the result to a numpy array to pass into an sklearn regression algorithm, so I will use the code above like this:
training_set = array(df[df.c > 0.5][locs])
... and that peeves me since I end up with a huge array copy in memory. Perhaps there's a better way for that too?
.loc
accept row and column selectors simultaneously (as do .ix/.iloc
FYI)
This is done in a single pass as well.
In [1]: df = DataFrame(np.random.rand(4,5), columns = list('abcde'))
In [2]: df
Out[2]:
a b c d e
0 0.669701 0.780497 0.955690 0.451573 0.232194
1 0.952762 0.585579 0.890801 0.643251 0.556220
2 0.900713 0.790938 0.952628 0.505775 0.582365
3 0.994205 0.330560 0.286694 0.125061 0.575153
In [5]: df.loc[df['c']>0.5,['a','d']]
Out[5]:
a d
0 0.669701 0.451573
1 0.952762 0.643251
2 0.900713 0.505775
And if you want the values (though this should pass directly to sklearn as is); frames support the array interface
In [6]: df.loc[df['c']>0.5,['a','d']].values
Out[6]:
array([[ 0.66970138, 0.45157274],
[ 0.95276167, 0.64325143],
[ 0.90071271, 0.50577509]])
这篇关于如何将列和行的 Pandas DataFrame 子集转换为 numpy 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!