【发布时间】:2026-01-24 04:40:01
【问题描述】:
我有如下数据集(这是一个例子,它实际上有 66k 行):
Type Food Loc Num
0 Fruit Banana House-1 15
1 Fruit Banana House-2 4
2 Fruit Apple House-2 6
3 Fruit Apple House-3 8
4 Vegetable Broccoli House-3 8
5 Vegetable Lettuce House-4 12
6 Vegetable Peppers House-5 3
7 Vegetable Corn House-4 4
8 Seasoning Olive Oil House-6 2
9 Seasoning Vinegar House-7 2
我想用 0 填充所有缺失的组合(3-7 号房屋有多少香蕉?除了 5 号房屋还有多少辣椒?),得到如下结果:
Type Food Loc Num
0 Fruit Banana House-1 15
1 Fruit Banana House-2 4
2 Fruit Banana House-3 0
... fill remaining houses with zeros
6 Fruit Banana House-7 0
7 Fruit Apple House-1 0
8 Fruit Apple House-2 6
9 Fruit Apple House-3 8
... fill remaining houses with zeros
14 Vegetable Broccoli House-1 0
15 Vegetable Broccoli House-2 0
16 Vegetable Broccoli House-3 8
... etc
n Seasoning Vinegar House-7 2
我知道 R 集成了 complete function。
现在我一直在处理从原始 DataFrame 中提取的列表,我将其转换为字典。
for key,grp in fruit.groupby(level=0):
dir[key] = test.ix[key].values.tolist()
fruit = {'Banana': [[1.0,15.0], [2.0,4.0],
'Apple': [[2.0,6.0], [3.0,8.0]
#Type = {fruit1:[[Loc1,Count1],...,[Locn],[Countn],
#... fruitn:[...]}
我设计了这个函数来应用字典的赋值规则:
def fill_zeros(list):
final = [0] * 127
for i in list:
final[int(i[0])] = i[1]
return final
这适用于单个“水果”:
print fill_zeros(test.ix['QLLSEEEKK'].values.tolist())
print fill_zeros(test.ix['GAVPLEMLEIALR'].values.tolist())
print fill_zeros(test.ix['VPVNLLNSPDCDVK'].values.tolist())
但字典上没有:
for key,grp in test.groupby(level=0):
dir[key] = fill_zeros(test.ix[key].values.tolist())
Traceback (most recent call last):
File "peptidecount.py", line 59, in <module>
print fill_zeros(test.ix[str(key)].values.tolist())
File "peptidecount.py", line 43, in fill_zeros
final[int(i[0])] = i[1]
TypeError: 'float' object has no attribute '__getitem__'
显然我没有在字典上正确迭代。有没有办法纠正它?还是有更合适的函数直接应用在DataFrame上?
【问题讨论】:
-
您可以发布所需的数据集吗?这将有助于了解你在追求什么......
标签: python pandas dictionary combinations