I want to match the consistency of data that is missing from Pandas' DataFrame

Asked 1 years ago, Updated 1 years ago, 329 views

Thank you for your continuous support.

when viewing data in Pandas.DataFrame such as display(df) Some data are as follows:

I would like to do the following, but what should I do?

  • Perhaps id and date are paired...
  • About before/after, if one side is NaN, I want to summarize it together
  • id=C and some records have both NaNs, or only one data exists

python pandas dataframe

2022-11-05 19:29

1 Answers

For the data frames listed in the question, the following is sufficient, but whether or not the data frames to be processed actually yield the desired results will probably be disappointing.

dfx=df.groupby(['id', 'date', group_keys=False)\
        .apply(lambdax:x.bfill().ffill().drop_duplicates())\
        .reset_index(drop=True)
               
print(dfx)


2022-11-05 19:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.