1 回答
TA贡献1840条经验 获得超5个赞
如果性能不是您最关心的问题,您可以将 Pandas 与Categorical Data和groupby. 这是有效的,因为默认情况下,groupby分类使用分类系列的笛卡尔积:
import pandas as pd, numpy as np
# construct dataframe
df = pd.DataFrame(array, columns=['city', 'language', 'value'])
# convert to categories
for col in ['city', 'language']:
df[col] = df[col].astype('category')
# groupby.first or groupby.sum works if you have unique combinations
res = df.sort_values(['city', 'language'])\
.groupby(['city', 'language']).first().fillna(0).reset_index()
print(res)
city language value
0 City1 English 0
1 City1 French 194
2 City1 Spanish 163
3 City2 English 1239
4 City2 French 456
5 City2 Spanish 1389
然后,对于您想要的列表输出列表:
res_lst = res.groupby('city')['value'].apply(list).tolist()
res_lst = [list(map(int, x)) for x in res_lst]
print(res_lst)
[[0, 194, 163], [1239, 456, 1389]]
添加回答
举报