Python Pandas:在所有单元格中选择数组中的第一个元素 [英] Python Pandas: selecting 1st element in array in all cells

查看:1000
本文介绍了Python Pandas:在所有单元格中选择数组中的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是选择每个单元格的第一个元素,而不考虑列或行的数量(它们可能会根据用户定义的标准而改变),并从数据中创建一个新的pandas数据框.我的实际数据结构与下面列出的类似.

What I am trying to do is select the 1st element of each cell regardless of the number of columns or rows (they may change based on user defined criteria) and make a new pandas dataframe from the data. My actual data structure is similar to what I have listed below.

       0       1       2
0   [1, 2]  [2, 3]  [3, 6]
1   [4, 2]  [1, 4]  [4, 6]
2   [1, 2]  [2, 3]  [3, 6]
3   [4, 2]  [1, 4]  [4, 6]

我希望新的数据框看起来像这样:

I want the new dataframe to look like:

    0   1   2
0   1   2   3
1   4   1   4
2   1   2   3
3   4   1   4

下面的代码生成一个类似于我的数据集,并尝试在我的代码中做我想做的但没有成功的数据(d),并且模仿了我在类似的问题中看到的成功(c)的情况;但是,只有一个柱子).指向类似但不同的问题的链接在这里: Python Pandas:在其中选择元素数组列

The code below generates a data set similar to mine and attempts to do what I want to do in my code without success (d), and mimics what I have seen in a similar question with success(c ; however, only one column). The link to the similar, but different question is here :Python Pandas: selecting element in array column

import pandas as pd

zz = pd.DataFrame([[[1,2],[2,3],[3,6]],[[4,2],[1,4],[4,6]],
               [[1,2],[2,3],[3,6]],[[4,2],[1,4],[4,6]]])
print(zz)

x= zz.dtypes
print(x)

a = pd.DataFrame((zz.columns.values))
b = pd.DataFrame.transpose(a) 
c =zz[0].str[0] # this will give the 1st value for each cell in columns 0
d= zz[[b[0]].values].str[0] #attempt to get 1st value for each cell in all columns

推荐答案

您可以使用apply,并使用使用 stack 的另一种解决方案和 unstack :

print (zz.stack().str[0].unstack())
   0  1  2
0  1  2  3
1  4  1  4
2  1  2  3
3  4  1  4

这篇关于Python Pandas:在所有单元格中选择数组中的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆