Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to merge dataframe in dataframe

I am using Rest API. I need data in the form of a data frame. For this, I am using pandas. The query parameter of Rest API is connected to Database, so I am getting multiple outputs from Rest API. I want to merge these outputs in the same data frame.
Anyone can help me ?

cur.execute("SELECT * from curd")
rows = cur.fetchall()
l = []
for row in rows:
 #print("ID = ", row[1], "\n")
  r = requests.get("http://localhost:8280/ID="+row[1], headers={uniquestr('Authorization'): 'Basic ',uniquestr('Authorization'): 'Basic'})
  #print("CONSUMER_ID = ", row[1], "\n")
   s = r.json()
  df = pd.DataFrame(s['R']['L'])
  df1 = df.groupby(pd.to_datetime(df.DateTime).dt.date).agg({'ACT': 'sum'}).reset_index()

and Code output is

                  DateTime  ACT_IMP_TOT
   0            2022-05-01       19.252
   1            2022-05-02       19.911
   2            2022-05-03       23.671
                 DateTime  ACT_IMP_TOT
   0            2022-05-01       37.352
   1            2022-05-02       27.780
   2            2022-05-03       28.557

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use pandas.concat():

pd.concat(df1, df2)

E.g. using pd.concat(df1, df2, df3):

enter image description here

So:

dfs = []

cur.execute("SELECT * from curd")
rows = cur.fetchall()
l = []
for row in rows:
 #print("ID = ", row[1], "\n")
  r = requests.get("http://localhost:8280/ID="+row[1], headers={uniquestr('Authorization'): 'Basic ',uniquestr('Authorization'): 'Basic'})
  #print("CONSUMER_ID = ", row[1], "\n")
   s = r.json()
  df = pd.DataFrame(s['R']['L'])
  df1 = df.groupby(pd.to_datetime(df.DateTime).dt.date).agg({'ACT': 'sum'}).reset_index()
  dfs.append(df1)
print(pd.concat(dfs))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading