Reputation: 25999
I'm struggling to create multiple rows from one in Pandas. For example, assuming I have this data:
df = pd.DataFrame({"Name": ["Bob", "Mary", "Amy"], "Age": [22, 35, 58], "EarningsYearly": [100,200,50],})
df['Quarterly'] = df['EarningsYearly']/4
df
Name Age EarningsYearly Quarterly
0 Bob 22 100 25.0
1 Mary 35 200 50.0
2 Amy 58 50 12.5
I can take early and turn it into quarterly but I actually want a new line for each so I can process each separately later. So my expected output is:
How can I do it? Also another similar question is can I generate the new lines within the command I used to create the Quarterely column? Or should I create it first then add new rows?
Upvotes: 2
Views: 714
Reputation: 23217
You can use .loc
+ df.index.repeat()
+ reset_index()
, as follows:
df_out = df.loc[df.index.repeat(4)].reset_index(drop=True)
Result
print(df_out)
Name Age EarningsYearly Quarterly
0 Bob 22 100 25.0
1 Bob 22 100 25.0
2 Bob 22 100 25.0
3 Bob 22 100 25.0
4 Mary 35 200 50.0
5 Mary 35 200 50.0
6 Mary 35 200 50.0
7 Mary 35 200 50.0
8 Amy 58 50 12.5
9 Amy 58 50 12.5
10 Amy 58 50 12.5
11 Amy 58 50 12.5
Upvotes: 0
Reputation: 153460
Use:
df.reindex(df.index.repeat(4)).reset_index(drop=True)
Output:
Name Age EarningsYearly Quarterly
0 Bob 22 100 25.0
1 Bob 22 100 25.0
2 Bob 22 100 25.0
3 Bob 22 100 25.0
4 Mary 35 200 50.0
5 Mary 35 200 50.0
6 Mary 35 200 50.0
7 Mary 35 200 50.0
8 Amy 58 50 12.5
9 Amy 58 50 12.5
10 Amy 58 50 12.5
11 Amy 58 50 12.5
Upvotes: 4