vishvas chauhan
vishvas chauhan

Reputation: 199

How to append new rows into a column of a dataframe based on conditions in python?

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{'cat': '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----

Upvotes: 1

Views: 1335

Answers (2)

Muhammad Hassan
Muhammad Hassan

Reputation: 4229

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)

Upvotes: 3

Corralien
Corralien

Reputation: 120429

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

Upvotes: 0

Related Questions