Home > Back-end >  How to add a new category into a column of a dataframe based on conditions in python?
How to add a new category into a column of a dataframe based on conditions in python?

Time:10-07

I want to add a new category into my existing column based on some conditions

Trial

df.loc[df['cat'] == 'woman','age'].max()-df.loc[df['cat'] == 'man','age'].max().apend{'gender': 'first_child', 'age': age}
import pandas as pd
d = {'cat': ['man1','man', 'woman','woman'], 'age': [30, 40, 50,55]}
df = pd.DataFrame(data=d)

print(df)

cat    age
man    30
man    40
woman  50
woman  55

output required:

cat         age
man          30
man          40
woman        50
woman        55
first_child  15
sec_child    10

Its possible by transpose but actual data is very complex

df.transpose()
cat man man woman   woman
age  30  40  50      55

---looking for solution in rows amend----

CodePudding user response:

Try:

import pandas as pd
d = {'cat': ['man1','man2', 'woman1','woman2'], 'age': [30, 40, 50,55]}
df = pd.DataFrame(data=d)

df_man = df[df.cat.str.startswith('man')].reset_index(drop=True)
df_woman = df[df.cat.str.startswith('woman')].reset_index(drop=True)

childs = [f'child{i}' for i in range(1, len(df_woman) 1)]
df_child = pd.DataFrame(data={'cat':childs, 'age': (df_woman['age'].sub(df_man['age'])).values})

df = pd.concat([df_man, df_woman, df_child], ignore_index=True)
print(df)

CodePudding user response:

I don't understand the logic behind this but you can use append:

age = df.loc[df['gender'] == 'female', 'age'].squeeze() \
      - df.loc[df['gender'] == 'male', 'age'].squeeze()

df = df.append({'gender': 'child', 'age': age}, ignore_index=True)

Output:

>>> df
   gender  age
0    male    3
1  female    4
2   child    1

CodePudding user response:

you can add a row using DataFrame.append()

like this

import pandas as pd
d = {'gender': ['male', 'female'], 'age': [3, 4]}
df = pd.DataFrame(data=d)

df
gender age
0 male
1 female
d2 = {'gender': ['child'], 'age': [1]}
d2 = pd.DataFrame(data=d2)
df = df.append(d2)
df
gender age
0 male
1 female
2 child
  • Related