简体   繁体   English

在 python 中的 Panda dataframe 中查找 numpy 数组的大小

[英]Find size of numpy array within a Panda dataframe in python

I would like to get the size of each numpy array within a panda.我想获得熊猫中每个 numpy 数组的大小。 How do I do this?我该怎么做呢? I have:我有:

          x          y                      z
0  [1, 2, 3, 4]  [8, 9, 7]              [8, 9, 7]
1  [2, 3, 4, 8]  [9, 8, 1]  [9, 8, 1, 6, 7, 8, 9]
2     [5, 6, 7]  [3, 4, 1]              [3, 4, 1]


cars= pd.DataFrame({'x': [[1,2,3,4],[2,3,4,8],[5,6,7]],
                    'y': [[8,9,7],[9,8,1],[3,4,1]],
                    'z': [[8,9,7],[9,8,1,6,7,8,9],[3,4,1]]})

I want:我想:

   x  y  z
0  4  3  3
1  4  3  7
2  3  3  3

I know how to get the shape and size of the entire DataFrame, but not how to combine them with size of each block.我知道如何获得整个 DataFrame 的形状和大小,但不知道如何将它们与每个块的大小结合起来。

print(cars)
print(cars.size)
print(cars.shape)

Use Series.str.len in DataFrame.apply for precessing all columns:使用Series.str.len中的DataFrame.apply处理所有列:

df = cars.apply(lambda x: x.str.len())
print (df)
   x  y  z
0  4  3  3
1  4  3  7
2  3  3  3

If no missing values use DataFrame.applymap for element-wise apply function len :如果没有缺失值,请使用DataFrame.applymap元素应用 function len

df = cars.applymap(len)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM