Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create new column in Pandas with condition to repeat by a value of another column?

I'm beginner in Python, I have a big DataFrame which looks like that:

import pandas as pd
df = pd.DataFrame({'Total': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], \
                    'Type': ['Child', 'Boy', 'Girl', 'Senior', '', '', '', '', '', ''], \
                    'Count': [4, 5, 1, 0, '', '', '', '', '', '']})
df[["Total", "Type", "Count"]]
df

Output:

   Total    Type    Count
0   10     Child    4
1   10       Boy    5
2   10      Girl    1
3   10     Senior   0
4   10      
5   10      
6   10      
7   10      
8   10      
9   10      

I want to have something like that:

    Total   Type    Count   New
0   10     Child       4    Child
1   10       Boy       5    Child
2   10      Girl       1    Child
3   10    Senior       0    Child
4   10                      Boy
5   10                      Boy
6   10                      Boy
7   10                      Boy
8   10                      Boy
9   10                      Girl

I don’t know how I can create a new column with a condition to repeat Type ntime as the number of Count.

Thanks!

like image 498
M-M Avatar asked Jun 07 '18 11:06

M-M


People also ask

How do I create a new column based on another column value in pandas?

Using apply() method If you need to apply a method over an existing column in order to compute some values that will eventually be added as a new column in the existing DataFrame, then pandas. DataFrame. apply() method should do the trick.

How do I create a new column based on multiple columns in pandas?

Create a new column by assigning the output to the DataFrame with a new column name in between the [] . Operations are element-wise, no need to loop over rows. Use rename with a dictionary or function to rename row labels or column names.

How do I create a conditional column in pandas?

You can create a conditional column in pandas DataFrame by using np. where() , np. select() , DataFrame. map() , DataFrame.


1 Answers

Using repeat, replace the blank to 0 in Count

df['New']=df.Type.repeat(df.Count.replace('',0)).values
df
Out[657]: 
  Count  Total    Type    New
0     4     10   Child  Child
1     5     10     Boy  Child
2     1     10    Girl  Child
3     0     10  Senior  Child
4           10            Boy
5           10            Boy
6           10            Boy
7           10            Boy
8           10            Boy
9           10           Girl
like image 97
BENY Avatar answered Sep 20 '22 15:09

BENY