我有如下所示的数据集(这是一个例子,它实际上有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我想填补所有缺失的组合(有多少香蕉在房子3-7?,有多少辣椒在其他地方的房子-5?)使用0,可以得到如下内容:
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有 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的功能?
发布于 2016-06-23 18:52:49
你可以用reindex。
首先,您需要一个有效(type, food)对的列表。我将从数据本身获取它,而不是将它们写出来。
In [88]: kinds = list(df[['Type', 'Food']].drop_duplicates().itertuples(index=False))
In [89]: kinds
Out[89]:
[('Fruit', 'Banana'),
('Fruit', 'Apple'),
('Vegetable', 'Broccoli'),
('Vegetable', 'Lettuce'),
('Vegetable', 'Peppers'),
('Vegetable', 'Corn'),
('Seasoning', 'Olive Oil'),
('Seasoning', 'Vinegar')]现在,我们将使用kinds为那些房屋生成所有的对。
In [93]: from itertools import product
In [94]: houses = ['House-%s' % x for x in range(1, 8)]
In [95]: idx = [(x.Type, x.Food, house) for x, house in product(kinds, houses)]
In [96]: idx[:2]
Out[96]: [('Fruit', 'Banana', 'House-1'), ('Fruit', 'Banana', 'House-2')]现在,您可以使用set_index和reindex来获取缺失的观测结果。
In [98]: df.set_index(['Type', 'Food', 'Loc']).reindex(idx, fill_value=0)
Out[98]:
Num
Type Food Loc
Fruit Banana House-1 15
House-2 4
House-3 0
House-4 0
House-5 0
... ...
Seasoning Vinegar House-3 0
House-4 0
House-5 0
House-6 0
House-7 2
[56 rows x 1 columns]发布于 2016-06-23 18:29:24
这应该是可行的:
cond0 = df.Num.isnull()
cond1 = df.Food == 'Banana'
cond2 = df.Loc.str.match(r'House-[34567]')
cond3 = df.Food == 'Peppers'
cond4 = df.Loc != 'House-5'
missing_bananas = cond0 & cond1 & cond2
missing_peppers = cond0 & cond3 & cond4
missing_food = missing_bananas | missing_peppers
df.loc[missing_food] = df.loc[missing_food].fillna(0)https://stackoverflow.com/questions/37998861
复制相似问题