简体   繁体   English

如何根据另一个日期时间列顺序并按 ID 分组从多个列中获取第一个非空值?

[英]How do I get the first non-null value from multiple columns based on another datetime column order and grouped by ID?

What I've got right now is a DataFrame like this:我现在得到的是这样的 DataFrame:

    id  ts                          site   type
0   111 2022-07-25 19:07:00.938365  A      NaN
1   111 2022-07-25 19:07:00.938371  NaN    1.0
2   222 2022-07-25 19:07:00.938372  NaN    NaN
3   222 2022-07-25 19:07:00.938373  NaN    2.0
4   222 2022-07-25 19:07:00.938374  C      1.0

What I'm trying to do is get the first non-null values of site and type for each id , based on the descending order of ts .我要做的是根据ts的降序获取每个idsitetype的第一个非空值。

So my expected output is something like:所以我预期的 output 是这样的:

    id  site   type
0   111 A      1.0
1   222 C      1.0

I've tried to do this:我试过这样做:

df_grouped = df.sort_values(by="ts", ascending=False).groupby("id").ffill().first()


> TypeError: first() missing 1 required positional argument: 'offset'

I've also tried this:我也试过这个:

df_grouped[["site", "type"]].apply(lambda x: x.first_valid_index()).reset_index()



    index       0
0   site        0
1   screen_type 0

You can do like this:你可以这样做:

df = df.sort_values('ts', ascending=False)

df.groupby('id', as_index=False)[['site', 'type']].agg(lambda x: x.dropna().iloc[0])

or using first_valid_index :或使用first_valid_index

df.groupby('id', as_index=False)[['site', 'type']].agg(lambda x: x[x.first_valid_index()])

output: output:

    id site  type
0  111    A   1.0
1  222    C   1.0

Note: If you have all NaNs in either 'site' or 'type' columns it won't work.注意:如果“站点”或“类型”列中的所有 NaN 都将不起作用。 Then you don't even have to do this probably.那么你甚至不必这样做。

(df.sort_values('ts', ascending=False).bfill().groupby('id')[['site', 'type']]
   .agg(lambda x:x.bfill().head(1)).reset_index())

    id site  type
0  111    A   1.0
1  222    C   1.0

Note that if YOU ARE SURE there is ATLEAST 1 NON-NAN per id then you can do:请注意,如果您确定每个 id 至少有 1 个 NON-NAN,那么您可以执行以下操作:

(df.sort_values('ts', ascending=False).bfill().groupby('id')[['site', 'type']]
   .first().reset_index())

    id site  type
0  111    A   1.0
1  222    C   1.0

暂无
暂无

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

相关问题 PySpark:获取数据框中每列的第一个非空值 - PySpark: Get first Non-null value of each column in dataframe 如何将ID分组并使用非空值标记第一行? - How can I groupby an ID and tag the first row with a non-null value? python数据框,基于一列的groupby并使用最后一个非空值填充另一列的空值 - python dataframe, groupby based on one column and fill null values from another column using last non-null value 如何从Python数据框的多个列中选择所有非NULL值 - How to pick out all non-NULL value from multiple columns in Python Dataframe 使用基于具有非空值的其他列的lambda在数据框中创建列 - Create a column in dataframe using lambda based on another columns with non-null values 熊猫根据另一列的非空值创建一个新列 - Pandas create a new column based on non-null value of another column 如何将 Pandas Dataframe 中某些列的非空值填充到新列中? 如何在多个条件下使用 np.where()? - How to fill Non-Null values from some columns in Pandas Dataframe into a new column? How to use np.where() for multiple conditions? Pandas - 在列中找到第一个非空值 - Pandas - find first non-null value in column Pandas 列列表中每行的第一个非空值 - First non-null value per row from a list of Pandas columns 从熊猫数据框中的多个列创建一个包含所有非空值的单个列 - create a single column containing all non-null values from multiple columns in a pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM