Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

python - merge one pandas dataframe to another and remove value which is present in first dataframe from second dataframe

I have two pandas data frame like below :-

DF1

     date1      hours   value    Field  count1
1   2021-01-15   9       eps       EPS          770915
2   2021-01-22   9       eps       EPS          797503

DF2

        date1       hours   value      Field    count1
  0     2021-01-09   9       eps        EPS     0
  1     2021-01-10   9       eps        EPS     0
  2     2021-01-11   9       eps        EPS     0
  3     2021-01-12   9       eps        EPS     0
  4     2021-01-13   9       eps        EPS     0
  5     2021-01-14   9       eps        EPS     0
  6     2021-01-15   9       eps        EPS     0
  7     2021-01-16   9       eps        EPS     0
  8     2021-01-17   9       eps        EPS     0
  9     2021-01-18   9       eps        EPS     0
 10     2021-01-19   9       eps        EPS     0
 11     2021-01-20   9       eps        EPS     0
 12     2021-01-21   9       eps        EPS     0
 13     2021-01-22   9       eps        EPS     0

I want output like below :-

Result :-

        date1       hours   value      Field    count1
  0     2021-01-09   9       eps        EPS     0
  1     2021-01-10   9       eps        EPS     0
  2     2021-01-11   9       eps        EPS     0
  3     2021-01-12   9       eps        EPS     0
  4     2021-01-13   9       eps        EPS     0
  5     2021-01-14   9       eps        EPS     0
  6     2021-01-15   9       eps        EPS     770915
  7     2021-01-16   9       eps        EPS     0
  8     2021-01-17   9       eps        EPS     0
  9     2021-01-18   9       eps        EPS     0
 10     2021-01-19   9       eps        EPS     0
 11     2021-01-20   9       eps        EPS     0
 12     2021-01-21   9       eps        EPS     0
 13     2021-01-22   9       eps        EPS     797503

data type of columns DF1 and DF2 are like :-

date1          object
hours           int64 
value          object
Field          object
count1          int64

The DF2 always contain 14 days and DF1 contain variable days which is between 1 to 14. I want result dataframe which contain missing days field with zero from 2nd dataframe.

question from:https://stackoverflow.com/questions/65845416/merge-one-pandas-dataframe-to-another-and-remove-value-which-is-present-in-first

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use concat with DataFrame.drop_duplicates:

df = (pd.concat([DF1, DF2], ignore_index=True)
        .drop_duplicates(['date1','hours','value','Field']))

If is necessary sorting output:

df = (pd.concat([DF1, DF2], ignore_index=True)
        .drop_duplicates(['date1','hours','value','Field'])
        .sorT_values(['date1','hours','value','Field']))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...