简体   繁体   中英

Pandas DataFrame combine rows by column value, where rows can have NaNs

I have a Pandas DataFrame like the following:

            timestamp       A       B       C       D       E       F
0           1607594400000   83.69   NaN     NaN     NaN     1003.20 8.66
1           1607594400000   NaN     2.57    44.35   17.18   NaN     NaN
2           1607595000000   83.07   NaN     NaN     NaN     1003.32 8.68
3           1607595000000   NaN     3.00    42.31   20.08   NaN     NaN
..          ...             ...     ...     ...     ...     ...     ...
325         1607691600000   90.19   NaN     NaN     NaN     997.32  10.22
326         1607691600000   NaN     1.80    30.10   14.85   NaN     NaN
328         1607692200000   NaN     1.60    26.06   12.78   NaN     NaN
327         1607692200000   91.33   NaN     NaN     NaN     997.52  10.21

I need to combine the rows that have the same value for timestamp, where in the cases where there is nan-value the value is maintained and in the cases where there is value-value the average of the values is calculated.

I tried the solution of the following question but it is not exactly my situation and I don't know how to addapt it: pandas, combine rows based on certain column values and NAN

Just use groupby :

df.groupby('timestamp', as_index=False).mean()

Try with first , it will pick the not null value for each group

out = df.groupby('timestamp', as_index=False).first()

Or

out = df.set_index('timestamp').mean(level=0)

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