简体   繁体   中英

Pandas data frame Concatenate and Sorting the Index

I have a dataframe with one categorical column, I divided dataframe to do manipulation depending on category now want to merge the dataset,

index A   B   C   
 1    M   2.2 3.4 
 2    F   3.4 1.0
 3    M   3.0 2.1
  .
  .
  .

created two new dataframe

index A   B   C   
 1    M   2.2 3.4 
 3    M   3.0 2.1
      .
      .
      .



   index A   B   C   
     2   F   3.4 1.0
          .
          .
          .

I want to get the resultant output as:

index A   B   C    D
 1    M   2.2 3.4  2.8
 2    F   3.4 1.0  3.4
 3    M   3.0 2.1  2.05
      .
      .
      .

I tried to do merge and concat but not getting desired solution.

You can create a list of dataframes and then concatenate all

import pandas as pd
frames = [df1, df2] # df1 and df2 are two dataframes
result = pd.concat(frames)

or you can simply use append

result = df1.append(df2)

A better way would be:

result = pd.concat([df1, df4], ignore_index=True, sort=False)

source: https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html

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