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!
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.
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.
You can create a conditional column in pandas DataFrame by using np. where() , np. select() , DataFrame. map() , DataFrame.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With