简体   繁体   English

如何在另一个数据帧 python pandas 中的多列上使用条件逻辑在数据帧中创建一列?

[英]How can I create a column in a dataframe using conditional logic on multiple columns in another dataframe python pandas?

I am trying to take the result value associated with the latest date in my dataframe, and create a new dataframe containing 'location', 'latest_date', and 'latest_result'.我正在尝试获取与数据框中最新日期相关的结果值,并创建一个包含“位置”、“最新日期”和“最新结果”的新数据框。 I have tried the following code:我尝试了以下代码:

 import pandas as pd import numpy as np df = pd.read_excel('SL_report_table.xlsx') df = df.dropna(subset=['RESULT']) df.head() LOCATION TYPE DATE EVENT RESULT D_RESULT FLAG UNITS 20 AS-01 NaN 2020-11-07 13:35:00 44142.565972 100.0 1.0 NaN ug/L 21 AS-01 NaN 2020-06-16 00:00:00 43998.000000 250.0 1.0 NaN ug/L 22 AS-01 NaN 2019-10-08 13:30:00 43746.562500 260.0 1.0 NaN ug/L 23 AS-01 NaN 2019-05-14 21:40:00 43599.902778 230.0 1.0 NaN ug/L 24 AS-01 NaN 2018-10-03 15:00:00 43376.625000 100.0 0.0 NaN ug/L grouped_maxdate = df.groupby('LOCATION').DATE.max() grouped_maxdate = grouped_maxdate.to_frame() for row in df: if row['LOCATION'] == grouped_maxdate['LOCATION'] and row['DATE'] == grouped_maxdate['LOCTION']: grouped_maxdate['LAST_RESULT'] = df['RESULT']

Any thoughts?有什么想法吗?

Sort values by DATE and keep the last row for each LOCATION group:DATE对值进行排序并保留每个LOCATION组的最后一行:

>>> df.sort_values('DATE').groupby('LOCATION').last()

          TYPE                 DATE         EVENT  RESULT  D_RESULT  FLAG UNITS
LOCATION
AS-01      NaN  2020-11-07 13:35:00  44142.565972   100.0       1.0   NaN  ug/L

Full code:完整代码:

out = df[['LOCATION', 'DATE', 'RESULT']].sort_values('DATE').groupby('LOCATION', as_index=False).last()

out.columns = ['location', 'latest_date', 'latest_result']
>>> out
  location          latest_date  latest_result
0    AS-01  2020-11-07 13:35:00          100.0

暂无
暂无

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

相关问题 如何在不使用 python 循环的情况下创建引用数据框和字典的当前列的条件列? - How can i create conditional column referring present columns of a dataframe and dictionary without using loop in python? 对熊猫数据框列使用条件if / else逻辑 - Using conditional if/else logic with pandas dataframe columns 熊猫数据框列上的条件逻辑 - Conditional Logic on Pandas Dataframe Column Python Pandas Dataframe - 创建新列 - Python Pandas Dataframe - Create new column using a conditional/applying a function based on another column 如何通过使用实际数据帧中两列中的值索引另一个数据帧来在实际数据帧中创建列 - How can I create a column in an actual dataframe by indexing another dataframe using the values in two columns from the actual dataframe 如何将列添加到 dataframe 中,其值取决于另一个 dataframe? - How can I add a column to a dataframe with a value conditional on another dataframe? 使用另一个数据框创建熊猫数据框列 - Create pandas dataframe column using another dataframe Python Pandas Dataframe将列创建为另一列中出现的字符串数 - Python Pandas Dataframe create column as number of occurrence of string in another columns 如何使用for循环在pandas数据框中的现有列上创建条件列 - How to create new column conditional on existing columns in pandas dataframe using for loop Pandas - 如何向数据框添加多个条件列? - Pandas - how to add multiple conditional columns to dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM