在条件 pandas python上遍历行 [英] iterate through rows on condition pandas python
本文介绍了在条件 pandas python上遍历行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个通过数据= pd.read_excel('.. data.xlsx')读取的excel表
I have an excel table that i read in by data = pd.read_excel('..data.xlsx')
Block Concentration Name Replicate Value
1 100 A 1 1446
1 100 A 2 25
1 100 A 3 12
1 33 A 1 111
1 33 A 2 222
1 33 A 3 1234
...
1 0 C 3 86
..
2 100 A 1 634
2 100 A 2 93
2 100 A 3 287
2 33 A 1 97234
2 33 A 2 1222
2 33 A 3 456
...
2 0 D 3 9800
...
...
24 0 E 3 93948
有24个方块,4种浓度和许多名称.每个块|浓度|名称"组合有三个重复项,它们指向唯一的值"数字.
There are 24 blocks, 4 types of concentrations and many names. There are three replicates for each 'Block|Concentration|Name' combo, which points to unique 'Value' numbers.
我创建了一个字典'd',其哈希值是:
I created a dictionary 'd' with hashes of :
hash{Block|Concentration|Name|Replicate} -> value
伪代码:
for block 1-> 24:
for each concentration:
print (concentration)
for each name:
for replicate 1-> 3:
print key of hash{Block|Concentration|Name|Replicate}
我的代码:
for b in data.Block:
for c in data.Concentration:
print(c)
for n in data.Name:
for r in data.Replicate:
print(d)
结果是一团糟,我认为它打印出每个循环的所有内容.
the result is a mess, i think it's printing out everything for each loop.
我想到的输出结构(输出不必采用格式):
the output structure i have in mind (output doesn't need to be in the format):
Block1
Concentration Name A Name B Name C..
100 1446 25 12 .. .. .. ...
33 111 222 1234 .. .. ..
10 .. .. .. ..
0 .. .. .. ..
Block2
Concentration Name A Name B Name C..
100 634 93 287 .. .. .. ...
33 97234 1222 456 .. .. ..
10 .. .. .. ..
0 .. .. .. ..
..
..
..
Block 24 ...
推荐答案
您可以使用 数据透视表
:
You can use pivot_table
:
In [11]: df
Out[11]:
Block Con Name Replicate Mean
0 1 100 A 1 20
1 1 100 A 2 10
2 1 100 A 3 30
3 1 100 B 1 40
4 1 100 B 2 12
5 1 100 B 3 23
6 1 33 A 1 56
7 1 33 A 2 234
8 1 33 A 3 377
9 1 33 B 1 434
10 1 33 B 2 1232
11 1 33 B 3 233
In [12]: df.pivot_table(index=["Block", "Con"], columns=["Name", "Replicate"], values="Mean")
Out[12]:
Name A B
Replicate 1 2 3 1 2 3
Block Con
1 33 56 234 377 434 1232 233
100 20 10 30 40 12 23
这篇关于在条件 pandas python上遍历行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文