python - Count frequency of a number in a column while equals to text in another column -
this sample csv
roughly looks 5 columns
clipboard:
year course modul q1 q2 2015 physics cs1203 4 2 2015 physics cs1203 4 3 2015 physics cs1203 3 1 2015 physics cs1203 4 4 2015 english ir0001 2 5 2015 english ir0001 1 2 2015 english ir0001 3 1 2015 english ir0001 5 3 2015 english ir0001 4 3
code:
df = pd.read_clipboard()
i grouped modules, want count number of 4s in module cs1203. new @ this, sorry in advance if stupid question. appreciate help.
thank you
i think need boolean indexing
:
print (df[(df.module == 'cs1203') & (df.q1 == 4)]) year course module q1 q2 0 2015 physics cs1203 4 2 1 2015 physics cs1203 4 3 3 2015 physics cs1203 4 4 print (len(df[(df.module == 'cs1203') & (df.q1 == 4)])) 3
if need count in q
columns first use melt
:
df = pd.melt(df, id_vars=['year','course','module'], value_name='q') year course module q1 q2 0 2015 physics cs1203 4 2 1 2015 physics cs1203 4 3 2 2015 physics cs1203 3 1 3 2015 physics cs1203 4 4 4 2015 english ir0001 2 5 5 2015 english ir0001 1 2 6 2015 english ir0001 3 1 7 2015 english ir0001 5 3 8 2015 english ir0001 4 3 print (df[(df.module == 'cs1203') & (df.q == 4)]) year course module variable q 0 2015 physics cs1203 q1 4 1 2015 physics cs1203 q1 4 3 2015 physics cs1203 q1 4 12 2015 physics cs1203 q2 4 print (len(df[(df.module == 'cs1203') & (df.q == 4)])) 4