jeudi 20 août 2020

Pandas - creating new column based on if condition

I have the following dataframe and I want to create a column 'poster' that shows the user if comment_id != np.nan and is np.nan if otherwise.

I have tried to do this with an if-statement in a for loop but instead of only getting the user name in the 'poster' column if there is an integer in the comment_id column, I get the user name in the 'poster' column for both integers and np.nan in the 'comment_id' column. There is probably only a small thing that is wrong, but I can't seem to figure it out.

Thanks a lot for helping me out!

d = {'comment_id':[1, np.nan, 2, np.nan, np.nan, 3],
     'user': ["Bob", "Ben", "Ben", "Charly", "Steve", "Tracy"]}

toydf = pd.DataFrame(d)

toydf['poster'] = np.nan

for n in toydf['comment_id']:
    if n != np.nan:
        toydf['poster']=toydf['user']
    else:
        toydf['poster']= np.nan

Aucun commentaire:

Enregistrer un commentaire