Home > front end >  Append a value to an existing column in a dataframe after conditions
Append a value to an existing column in a dataframe after conditions

Time:03-03

I want to append new values to an existing column with values in a dataframe.

Example: This is the initial dataframe

name    gender   age  
harry     M      10    
martha    F      11    

After checking conditions the data frame expands and adds a new column with values:

name    gender   age   remarks
harry     M      10    is_boy 
martha    F      11    is_girl

and again the process continues

This is the Final dataframe

name    gender   age   remarks
harry     M      10    is_boy | iowa | 2008
martha    F      11    is_girl| florida | 2007

This is a bit tricky for me as I have spend a lot of time behind this.

Would like to know how the code will look like.

CodePudding user response:

Assuming s the new Series at each loop, simply add the new data :

for s in function_generating_new_data():
    # only if type is unsure
    s = s.astype(str)
    if 'remarks' in df:
        # add data
        df['remarks']  = ' | ' s
    else:
        df['remarks'] = s

CodePudding user response:

For new column "remarks" there are multiple ways:

list comprehension:

df["remarks"] = ["is_boy" if gender =="M" else "is_girl" for gender in df['gender']]

np.where:

df["remarks"] = np.where(df["gender"]=="M","is_boy","is_girl")

You can see other methods from this link.

I assume the information "state | year" is in a list.

The simplest way:

additional_info = ["iowa | 2008","florida | 2007"]
df["remarks"] = df["remarks"]   " | "   additional_info
  • Related