Muhammad Jalal
Muhammad Jalal

Reputation: 21

How to add rows from one dataframe to another dataframe one after another using pandas

i have two dataframe like df1

    time    kw
0   13:00   30
1   13:02   28
2   13:04   29

and df2

  time    kw
1   13:01   30
2   13:03   28
3   13:05   29

i want to add rows from one dataframe to another for end result like

  time    kw
1   13:00   30
2   13:01   30
3   13:02   28
4   13:03   28
5   13:04   29
6   13:05   29

Please help.. I concat both dataframeresult_df = pd.concat([df1, df2]), but it just put them side by side. Secondly i tried to append both dataframe, but still not what i am looking for Thanks in advance

Upvotes: 1

Views: 3415

Answers (2)

Hussein Awala
Hussein Awala

Reputation: 5096

import pandas as pd

df1 = pd.DataFrame([("13:00", 30), ("13:02", 28), ("13:04", 29)], columns=["time", "kw"])
df2 = pd.DataFrame([("13:01", 30), ("13:03", 28), ("13:05", 29)], columns=["time", "kw"])

df = pd.concat([df1, df2]).sort_values("time")

Upvotes: 3

Mayank Porwal
Mayank Porwal

Reputation: 34086

Use df.append with df.sort_values:

In [2362]: df1.append(df2).sort_values('time')
Out[2362]: 
    time  kw
0  13:00  30
1  13:01  30
1  13:02  28
2  13:03  28
2  13:04  29
3  13:05  29

Upvotes: 1

Related Questions