i have a dataset that i read in with pd.read_excel
Block Con
1 100
1 100
1 100
1 33
1 33
1 33
2 100
2 100
2 100
2 33
2 33
2 33
...
there are a total of 10 'block's, each 'block' has 2 types of 'con': 100 and 33. how can i iterate through the 'Block' column so that for each 'block' it prints out the 2 types of 'con': 100 and 33
desire output:
1 100
33
2 100
33
my code:
for b in data.Block:
for c in data.Con:
print(c)
but it prints out all of the con for each row of block.
Use drop_duplicates:
In [11]: df
Out[11]:
Block Con
0 1 100
1 1 100
2 1 100
3 1 33
4 1 33
5 1 33
6 2 100
7 2 100
8 2 100
9 2 33
10 2 33
11 2 33
In [12]: df.drop_duplicates()
Out[12]:
Block Con
0 1 100
3 1 33
6 2 100
9 2 33
I would use groupby
in this way:
d = df.groupby(['Block','Con']).size()
which returns:
Block Con
1 33 3
100 3
2 33 3
100 3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.