简体   繁体   English

如何计算数据框中字典的行数?

[英]How to count the number of rows of a dictionary within a dataframe?

I am creating a project where I need to find out the number of rows of purchase orders in our system. 我正在创建一个项目,需要在我们的系统中找出采购订单的行数。 Thus allowing me to edit the POs by appending another row to the order. 因此,我可以通过在订单后追加另一行来编辑订单。

The way the data is stored is as follows: 数据的存储方式如下:

id  rows
0   49696   [{'productID': 57174, 'serviceID': 0, 'itemNam...
1   49691   [{'productID': 4036, 'serviceID': 0, 'itemName...
2   49690   [{'productID': 4036, 'serviceID': 0, 'itemName...
3   54827   [{'productID': 155099, 'serviceID': 0, 'itemNa...
4   54826   [{'productID': 155099, 'serviceID': 0, 'itemNa...
5   54825   [{'productID': 155099, 'serviceID': 0, 'itemNa...
6   54824   [{'productID': 155099, 'serviceID': 0, 'itemNa...
7   54823   [{'productID': 155098, 'serviceID': 0, 'itemNa...
8   54822   [{'productID': 155098, 'serviceID': 0, 'itemNa...
9   54821   [{'productID': 155098, 'serviceID': 0, 'itemNa...
10  54820   [{'productID': 155098, 'serviceID': 0, 'itemNa...
11  53580   [{'productID': 153856, 'serviceID': 0, 'itemNa...
12  53576   [{'productID': 153856, 'serviceID': 0, 'itemNa...
13  53572   [{'productID': 153856, 'serviceID': 0, 'itemNa...
14  53567   [{'productID': 153856, 'serviceID': 0, 'itemNa...
15  53560   [{'productID': 153273, 'serviceID': 0, 'itemNa...

What I want to be able to do is to query each row and find out the number of rows of each dictionary there is, hence number of rows per PO and append that as a new series to my DataFrame (nrRows). 我想要做的是查询每一行并找出每本词典的行数,因此每个PO的行数并将其作为新系列附加到我的DataFrame(nrRows)中。

I can't figure out the bet way to query the dictionary of rows on each row within my DataFrame... I know how to unpack them into a DataFrame each but I can't figure the most efficient way to return just the number of rows and append it to my original DF. 我无法弄清楚在我的DataFrame中查询每一行的行字典的下注方式...我知道如何将它们分别解压缩到DataFrame中,但是我无法找出最有效的方法来仅返回数字行并将其附加到我的原始DF中。

Any help much appreciated. 任何帮助,不胜感激。

Not sure if I fully understand the issue, but could you be looking for something like: 不知道我是否完全理解这个问题,但是您是否可以寻找这样的东西:

my_df["rows"].apply(lambda r: len(r))  

"Number of rows of a dictionary" is the part that is confusing. “词典的行数”是令人困惑的部分。 How do you define rows here? 您如何在此处定义行?

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

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