简体   繁体   English

如何合并具有相同索引的多行,每一行在熊猫中只有一个真实值?

[英]How to combine multiple rows with the same index with each row have only one true value in pandas?

I have a pandas dataframe which has the following shape: 我有一个熊猫数据框,其形状如下:

                          OPEN_INT PX_HIGH PX_LAST VOL
timestamp  ticker source     
2018-01-01   AAPL   NYSE         1      NaN    NaN NaN
2018-01-01   AAPL   NYSE       NaN        2    NaN NaN
2018-01-01   AAPL   NYSE       NaN      NaN      3 NaN
2018-01-01   AAPL   NYSE       Nan      NaN    NaN   4
2018-01-01   MSFT   NYSE         5      NaN    NaN NaN
2018-01-01   MSFT   NYSE       NaN        6    NaN NaN
2018-01-01   MSFT   NYSE       NaN      NaN      7 NaN
2018-01-01   MSFT   NYSE       Nan      NaN    NaN   8

In each column for each (timestamp, ticker, source) group there is gurantted only one value, all other values are Nan, is there any way I can combine these into single rows so it looks like: 在每个组(时间戳,行情指示器,源)的每一列中,仅保证一个值,所有其他值均为Nan,是否有任何方法可以将它们组合成单个行,因此如下所示:

                          OPEN_INT PX_HIGH PX_LAST VOL
timestamp  ticker source     
2018-01-01   AAPL   NYSE         1      2        3   4
2018-01-01   MSFT   NYSE         5      6        7   8

I have tried to use df.groupby(['timestamp', 'ticker', 'source']).agg(lambda x: x.dropna() but I got an error saying Function does not reduce . 我尝试使用df.groupby(['timestamp', 'ticker', 'source']).agg(lambda x: x.dropna()但出现错误,提示Function does not reduce

Use GroupBy.first : 使用GroupBy.first

df.groupby(['timestamp', 'ticker', 'source']).first()

If is always only one value per groups aggregate by max , min , sum , mean ...: 如果总是,则每组中只有一个值通过maxminsummean ...聚合:

df.groupby(['timestamp', 'ticker', 'source']).max()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何组合 pandas dataframe 中在一列中具有相同值的行 - How to combine rows in a pandas dataframe that have the same value in one column 如何使用pandas在同一行索引下有多行 - How to have multiple rows under the same row index using pandas 如何在 pandas 中将多行合并为一行 - how to combine multiple rows to one row in pandas 如何将每列只有 1 个非空条目的 Pandas 数据框中的多行合并为一行? - How to combine multiple rows in a pandas dataframe which have only 1 non-null entry per column into one row? 将多个分类列合并为一个,当每一行只有一个非 NaN 值时,在 Pandas - Combine multiple categorical columns into one, when each row has only one non-NaN value, in Pandas Pandas 将多行合并为一行,有条件 - Pandas combine multiple rows into one row with condition Pandas - 将多个组行合并为一行 - Pandas - Combine multiple group rows into one row 如何将同一类别的多行合并为大熊猫? - How to combine multiple rows of same category to one in pandas? 如何通过 pandas 数据帧 go 并仅保留在整个行中具有相同值的行? - How to go through a pandas data frame and only keep rows that have the same value throughout the entire row? 如何在 pandas 中使用 id 将多行合并为一行多列(将具有相同 id 的多条记录聚集到一条记录中) - How to combine multiple rows into a single row with many columns in pandas using an id (clustering multiple records with same id into one record)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM