I'm trying to query a dataframe, it looks like this
import numpy as np
import pandas as pd
data = [['a.txt', 1, 2, 2, np.NaN, np.NaN],
['b.txt', 2, np.NaN, 2, np.NaN, np.NaN],
['c.txt', 3, np.NaN, 2, np.NaN, np.NaN],
['d.txt', 2, 1, np.NaN, 3, np.NaN]]
df = pd.DataFrame(data, columns = ['file', 'type', 'banana', 'apple', 'orange', 'cherry'])
i'd like to be able to find out how many files contain cherries and apples, apples only and oranges and bananas. Then split them up by type.
so far I have
cher_and_apps = df.loc[(df.cherries>0) & (df.apples>0)]
oran_ban = df.loc[(df.oranges>0) & (df.banana>0)]
but im struggling for the apples only without having to write a long statement like
apples_only = df.loc[(df.cherries= np.NaN) & (df.apples > 0) etc...
I then want the output to look like
Type | Apples Only | Oranges & Bananas | Cherries & Apples
1 | 0 | 0 | 0
2 | 1 | 1 | 0
3 | 1 | 0 | 0
My plan was to split the original df into the above, then do a groupby. does this sound ok?