2017-03-20 89 views
0

我的数据框中有数组需要计数。计数单元格内的数组python

这是我使用的代码:

indi = data_1.query("'2016-11-22' <= login_date <= '2016-12-22'").groupby(['employer_key','account_id','login_date']).count().reset_index() 
indi_1 = indi.groupby(['employer_key']).account_id.unique().reset_index() 
indi_1 

,给了我这样的:

employer_key  account_id 
0 boeing    [17008601, 17008645, 17008698, 17008952, 17009...] 
1 dell_inc   [10892711, 10892747, 10894032, 10894676, 10894...] 
2 google    [9215462, 9216605, 9217052, 9218693, 9222937, ...] 
3 sprint_corporation [9858036, 9858809, 9859191, 9859350, 9859498, ...] 
4 walmart    [2515412, 2517367, 2519765, 2520049, 2526763, ...] 

我想在阵列中的数数,所以它看起来是这样的:

employer_key   account_id 
0 boeing    5000 
1 dell_inc   289 
2 google    789 
3 sprint_corporation 154670 
4 walmart   4689 

我该怎么做?我在用熊猫。我对python也很陌生,越简单越好。

回答

2

如果ACCOUNT_ID列包含列表,您可以使用str.len()来计算每个单元的元素个数:

df['account_id_count'] = df.account_id.str.len() 
df 

enter image description here

+0

它的工作!谢谢! – CoffeeCoffeeBuzzBuzz