1

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----

5
  • 1
    so you just want to add a new row? pandas.pydata.org/docs/reference/api/… Commented Oct 6, 2021 at 21:59
  • Where is "child" coming from? Also "child" isn't a gender...? Commented Oct 6, 2021 at 22:01
  • Do you mean that you want to subtract the female age column by the male age column? And append a new row? What should be done when there are more than two rows in your dataframe? Commented Oct 6, 2021 at 22:02
  • Yes, I want to add new rows. @Mahrkeenerh Commented Oct 7, 2021 at 7:16
  • I changed the col title and added more rows. Actual data is has mulitple unique value and calculation is done in same way. @ddejohn Please see if you can help. Commented Oct 7, 2021 at 7:19

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. Can you please answer this.stackoverflow.com/questions/69501747/…
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.