How I can count if a country that is in more rows , has failed or passed, enter image description here
Like is
JavaScript
x
9
1
ID unique Countries Test
2
1 Spain, Netherlands Fail
3
2 Italy Pass
4
3 France, Netherlands Pass
5
4 Belgium, France, Bulgaria Fail
6
5 Belgium, United Kingdom Pass
7
6 Netherlands, France Pass
8
7 France, Netherlands, Belgiu Pass
9
and the result should be like this enter image description here
JavaScript
1
8
1
Pass Fail
2
Spain 0 1
3
Italy 1 0
4
France 3 1
5
Netherlands 3 1
6
Belgium 2 1
7
United Kingdom 1 0
8
Because Netherlands is in 4 rows , and has 3 passed and one failed.
Advertisement
Answer
Use Series.str.split
with DataFrame.explode
and last call crosstab
:
JavaScript
1
3
1
df1 = df.assign(Countries = df.Countries.str.split(', ')).explode('Countries')
2
df2 = pd.crosstab(df1['Countries'],df1['Test'])
3