I need to duplicate a row based on a column (a number).
I have this dataframe:
Kids City Preferred Transport Count
0 Sofia Boston Bus 1.0
1 Claire Ann NaN NaN 1.0
2 Joe Detroit Train 3.0
3 Betty NaN Car 2.0
4 Archie Bruxelles NaN 1.0
5 Joe NaN Airplane 3.0
6 Phil Berlin Ship 1.0
7 Luke NaN Airplane 1.0
Each kid has a count number. It should appear in the dataframe as much as the number from the Count column shows. (Ex: it should only be 1 Sofia, 2 Bettys, and 3 Joes). But only have their names copied, like this:
Kids City Preferred Transport Count
0 Sofia Boston Bus 1.0
1 Claire Ann NaN NaN 1.0
2 Joe Detroit Train 3.0
3 Betty NaN Car 2.0
4 Archie Bruxelles NaN 1.0
5 Joe NaN Airplane 3.0
6 Phil Berlin Ship 1.0
7 Luke NaN Airplane 1.0
8 Joe NaN NaN 3.0
9 Betty NaN NaN 2.0
I tried this code, using Pandas:
kid = nyc_trip_df.loc[nyc_trip_df['Kids'].count() > nyc_trip_df['Count'], 'Kids']
Because I thought that I would be a good idea to put all the kids that have their names appear less then their corresponding number, but somewhere I failed. And if it was successfull, then I would just append the kids to the dataframe, but I am not sure that this is the best approach though....
Can you help me please?
How can I make this work ?