Home > Software design >  Append rows just beneath an excel using Pandas
Append rows just beneath an excel using Pandas

Time:04-25

First data frame is:

a b c
1 2 3

Second data frame is:

a1 b c1
4 5 6

What I expect is:

a b c
1 2 3
4 5 6

Just move all values from df2 in df1, the column names may be different.

CodePudding user response:

One option is to concatenate the underlying numpy arrays and build a DataFrame:

out = pd.DataFrame(np.r_[df1.to_numpy(), df2.to_numpy()], columns=df1.columns)

Output:

   a  b  c
0  1  2  3
1  4  5  6

CodePudding user response:

Create same columns names in both DataFrames and use concat:

df2.columns = df1.columns

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

Or:

df = pd.concat([df1, df2.rename(columns=dict(zip(df2.columns, df1.columns)))])
  • Related