mercredi 10 avril 2019

create a new dataframe based on conditions from the existing dataframe

I want to create a new dataframe with one column containing either 0 or 1 depending on the condition from the existing dataframe.

In df (existing) dataframe I want to check if values in columns a,b and c contain zeros simultaneously. If so, create a new dataframe with one column that returns zero if the condition is met. Otherwise return 1.

Example of existing dataframe:

df = pd.DataFrame({"a":[0,1,0,0],"b":[0,None,0,None],"c":[0,8,0,10],"new_col":[5,5,5,5]})
mylist = ['a','b','c']

if any((df[mylist] == 0).all(1).values):
    print('all zeros')
else:
    print('contains nonzero')

I cannot figure out what should I put in place of print statements in order to get the desired results.

Desired dataframe:

df_new = pd.DataFrame({'col':[0,1,0,1]})

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire