Append multiple pandas data frames at once
Question:
I am trying to find some way of appending multiple pandas data frames at once rather than appending them one by one using
df.append(df)
Let us say there are 5 pandas data frames t1
, t2
, t3
, t4
, t5
. How do I append them at once? Something equivalent of
df = rbind(t1,t2,t3,t4,t5)
Answers:
Have you simply tried using a list as argument of append? Or am I missing anything?
import numpy as np
import pandas as pd
dates = np.asarray(pd.date_range('1/1/2000', periods=8))
df1 = pd.DataFrame(np.random.randn(8, 4), index=dates, columns=['A', 'B', 'C', 'D'])
df2 = df1.copy()
df3 = df1.copy()
df = df1.append([df2, df3])
print df
#Row wise appending
combined_data = pd.concat([t1, t2, t3, t4, t5], axis=0)
This will stack one dataframe over another
#column wise appending
combined_data = pd.concat([t1, t2, t3, t4, t5], axis=1)
This will append the 2nd dataframe on the right side of the 1st dataframe
Suppose we have one main Excel file in which we have 3 sub-sheets named by p1
,p2
& p3
We can read all the sheets together as follows:
d = pd.read_excel('p1.xlsx',sheet_name=['p1','p2','p3'])
Now to combine all sheets in one data frame, we can use the following:
df=pd.concat(d[frame] for frame in d.keys())
if the columns each data frame is different you can add for to append :
#list dataframe you want to append
frame = [t1, t2, t3, t4, t5]
#new dataframe to store append result
myDataFrame = pd.DataFrame()
for df in frame:
myDataFrame = myDataFrame.append(df)
make sure append success by checking the myDataFrame lenght with :
len(myDataFrame)
if all columns in the data frame is the same or the columns difference each data frame will not be a concern as long as the number of columns of each data frame is the same you can use pd.concat(dataframe)
as mention by jezrael.
for more info about append and concat, please click link below :
Merge, join, concatenate and compare in Pandas
I am trying to find some way of appending multiple pandas data frames at once rather than appending them one by one using
df.append(df)
Let us say there are 5 pandas data frames t1
, t2
, t3
, t4
, t5
. How do I append them at once? Something equivalent of
df = rbind(t1,t2,t3,t4,t5)
Have you simply tried using a list as argument of append? Or am I missing anything?
import numpy as np
import pandas as pd
dates = np.asarray(pd.date_range('1/1/2000', periods=8))
df1 = pd.DataFrame(np.random.randn(8, 4), index=dates, columns=['A', 'B', 'C', 'D'])
df2 = df1.copy()
df3 = df1.copy()
df = df1.append([df2, df3])
print df
#Row wise appending
combined_data = pd.concat([t1, t2, t3, t4, t5], axis=0)
This will stack one dataframe over another
#column wise appending
combined_data = pd.concat([t1, t2, t3, t4, t5], axis=1)
This will append the 2nd dataframe on the right side of the 1st dataframe
Suppose we have one main Excel file in which we have 3 sub-sheets named by p1
,p2
& p3
We can read all the sheets together as follows:
d = pd.read_excel('p1.xlsx',sheet_name=['p1','p2','p3'])
Now to combine all sheets in one data frame, we can use the following:
df=pd.concat(d[frame] for frame in d.keys())
if the columns each data frame is different you can add for to append :
#list dataframe you want to append
frame = [t1, t2, t3, t4, t5]
#new dataframe to store append result
myDataFrame = pd.DataFrame()
for df in frame:
myDataFrame = myDataFrame.append(df)
make sure append success by checking the myDataFrame lenght with :
len(myDataFrame)
if all columns in the data frame is the same or the columns difference each data frame will not be a concern as long as the number of columns of each data frame is the same you can use pd.concat(dataframe)
as mention by jezrael.
for more info about append and concat, please click link below :
Merge, join, concatenate and compare in Pandas