简体   繁体   中英

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 dataframe result_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

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
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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM