本文介绍了列出中的Pandas DataFrame列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫 DataFrame 像这样

                          clusters
0                              [4]
1                  [9, 14, 16, 19]
2           [6, 7, 10, 17, 18, 20]
3  [1, 2, 3, 5, 8, 11, 12, 13, 15]

我只需要获取整数群集列。像下面这样(这可以是四个列表,不需要另一个 DataFrame

I need to get only the integer values in the cluster column separately. Like following(This can be four lists no need of having another DataFrame)

0                              4
1                  9, 14, 16, 19
2           6, 7, 10, 17, 18, 20
3  1, 2, 3, 5, 8, 11, 12, 13, 15

我尝试了不同的方法。无法达到预期的输出。

I tried different things. Could not achieve the expected output.

In [36]: clustlist = list(firstclusters.clusters.values)
Out[36]:
    [array([4]), array([ 9, 14, 16, 19]), array([ 6,  7, 10, 17, 18, 20]), array([ 1,  2,  3,  5,  8, 11, 12, 13, 15])]

In [37]: np.ravel(clustlist)
Out[37]:
    [array([4]) array([ 9, 14, 16, 19]) array([ 6,  7, 10, 17, 18, 20])
     array([ 1,  2,  3,  5,  8, 11, 12, 13, 15])]

In [38]: np.hstack(clustlist)
Out[38]:
    [ 4  9 14 16 19  6  7 10 17 18 20  1  2  3  5  8 11 12 13 15]


推荐答案

如果每个项目只是一个列表,则可以使用tolist系列方法:

If each item is just a list, you can use the tolist Series method:

In [11]: df.clusters.tolist()
Out[11]: [[4], [9, 14, 16, 19], [6, 7, 10, 17, 18, 20], [1, 2, 3, 5, 8, 11, 12, 13, 15]]

或者,如果这些是numpy数组,您需要首先对每个项目应用列表:

Or, if these are numpy arrays you need to apply tolist to each item first:

In [12]: df.clusters.apply(np.ndarray.tolist).tolist()
Out[12]: [[4], [9, 14, 16, 19], [6, 7, 10, 17, 18, 20], [1, 2, 3, 5, 8, 11, 12, 13, 15]]

这篇关于列出中的Pandas DataFrame列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 13:44