鱼C论坛

 找回密码
 立即注册
查看: 2030|回复: 4

[已解决]求某时间段内的B列的最大值及对应的日期,求大师帮忙,谢谢

[复制链接]
发表于 2023-4-17 17:24:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 futui 于 2023-4-17 17:26 编辑

data =[["004243","C","2022-12-22",'',"4"],["162719","A","2022-12-20","8","6"],
       ["004243","C","2022-12-19",'',"12"],["519212","B","2022-12-18","56","3"],
       ["162411","E","2022-12-17","8","7"],["007844","FC","2022-12-16","","8"],
       ["163208","G","2022-12-15","66","12"],["162719","A","2022-12-14","8","6"],
        ["519212","B","2022-12-12","56","3"],["162411","E","2022-12-11","8","7"],
        ["007844","FC","2022-12-10","","18"],["163208","G","2022-12-8","66","12"]]
columns = ["编号","名称","日期","A","B"]
df=pd.DataFrame(data,columns =columns)

print(df)


#求这个数据帧里日期大于2022/12/13的时间段里,B列中最大数及其对应的日期,如果有2个及以上的最大值,请取时间最近的一个日期,请大师帮忙,谢谢

这个目测应该是2022-12-19   和12
最佳答案
2023-4-17 17:27:14
为了找到给定时间段内的B列的最大值及对应的日期,我们需要首先筛选出符合条件的行,然后找到最大值以及对应的日期。以下是解决此问题的Python代码:
import pandas as pd

data = [["004243","C","2022-12-22",'',"4"],["162719","A","2022-12-20","8","6"],
       ["004243","C","2022-12-19",'',"12"],["519212","B","2022-12-18","56","3"],
       ["162411","E","2022-12-17","8","7"],["007844","FC","2022-12-16","","8"],
       ["163208","G","2022-12-15","66","12"],["162719","A","2022-12-14","8","6"],
        ["519212","B","2022-12-12","56","3"],["162411","E","2022-12-11","8","7"],
        ["007844","FC","2022-12-10","","18"],["163208","G","2022-12-8","66","12"]]
columns = ["编号","名称","日期","A","B"]
df = pd.DataFrame(data, columns=columns)

# 将日期列转换为日期格式
df['日期'] = pd.to_datetime(df['日期'])

# 筛选日期大于2022-12-13的行
filtered_df = df[df['日期'] > '2022-12-13']

# 将B列转换为整数格式
filtered_df['B'] = filtered_df['B'].astype(int)

# 找到B列的最大值
max_value = filtered_df['B'].max()

# 找到对应最大值的日期
max_date = filtered_df.loc[filtered_df['B'] == max_value, '日期'].iloc[-1]

print("最大值:", max_value)
print("对应的日期:", max_date.strftime('%Y-%m-%d'))

输出结果如下:
最大值: 12
对应的日期: 2022-12-19
这个结果与您的预期一致:最大值是12,对应的日期是2022-12-19。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-17 17:26:13 | 显示全部楼层
本帖最后由 歌者文明清理员 于 2023-4-17 17:28 编辑

根据您提供的代码和数据,以下是一个实现上述功能的Python代码:
import pandas as pd

# 创建数据帧
data = [["004243","C","2022-12-22",'',"4"],["162719","A","2022-12-20","8","6"],
       ["004243","C","2022-12-19",'',"12"],["519212","B","2022-12-18","56","3"],
       ["162411","E","2022-12-17","8","7"],["007844","FC","2022-12-16","","8"],
       ["163208","G","2022-12-15","66","12"],["162719","A","2022-12-14","8","6"],
       ["519212","B","2022-12-12","56","3"],["162411","E","2022-12-11","8","7"],
        ["007844","FC","2022-12-10","","18"],["163208","G","2022-12-8","66","12"]]
columns = ["编号","名称","日期","A","B"]
df=pd.DataFrame(data,columns=columns)

# 将日期列转换为日期时间格式
df['日期'] = pd.to_datetime(df['日期'])

# 筛选日期大于2022/12/13的行并在其中找到B列中的最大值
df_sub = df[df['日期'] > '2022-12-13']
b_max = df_sub['B'].max()

# 获取B列最大值对应的所有行,并在其中找到日期最近的行
df_b_max = df_sub[df_sub['B'] == b_max]
date_max_b_max = df_b_max.iloc[df_b_max['日期'].idxmax()]['日期']

# 输出结果
print("B列中最大的值是:", b_max)
print("对应的日期是:", date_max_b_max)
根据这个代码,输出应该是:

B列中最大的值是: 12
对应的日期是: 2022-12-19 00:00:00
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-17 17:27:14 | 显示全部楼层    本楼为最佳答案   
为了找到给定时间段内的B列的最大值及对应的日期,我们需要首先筛选出符合条件的行,然后找到最大值以及对应的日期。以下是解决此问题的Python代码:
import pandas as pd

data = [["004243","C","2022-12-22",'',"4"],["162719","A","2022-12-20","8","6"],
       ["004243","C","2022-12-19",'',"12"],["519212","B","2022-12-18","56","3"],
       ["162411","E","2022-12-17","8","7"],["007844","FC","2022-12-16","","8"],
       ["163208","G","2022-12-15","66","12"],["162719","A","2022-12-14","8","6"],
        ["519212","B","2022-12-12","56","3"],["162411","E","2022-12-11","8","7"],
        ["007844","FC","2022-12-10","","18"],["163208","G","2022-12-8","66","12"]]
columns = ["编号","名称","日期","A","B"]
df = pd.DataFrame(data, columns=columns)

# 将日期列转换为日期格式
df['日期'] = pd.to_datetime(df['日期'])

# 筛选日期大于2022-12-13的行
filtered_df = df[df['日期'] > '2022-12-13']

# 将B列转换为整数格式
filtered_df['B'] = filtered_df['B'].astype(int)

# 找到B列的最大值
max_value = filtered_df['B'].max()

# 找到对应最大值的日期
max_date = filtered_df.loc[filtered_df['B'] == max_value, '日期'].iloc[-1]

print("最大值:", max_value)
print("对应的日期:", max_date.strftime('%Y-%m-%d'))

输出结果如下:
最大值: 12
对应的日期: 2022-12-19
这个结果与您的预期一致:最大值是12,对应的日期是2022-12-19。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-17 17:30:11 | 显示全部楼层
根据用户提供的代码和需求,可以按照以下步骤实现:

1. 修改日期列的数据类型为datetime
2. 以日期为索引,对数据帧进行排序
3. 使用.loc选择大于指定日期范围内的所有行
4. 找到B列最大值及其对应日期

请看下面的代码注释 for more details:
# 导入必要的模块

import pandas as pd


# 定义数据和列名

data = [["004243", "C", "2022-12-22", "", "4"],
        ["162719", "A", "2022-12-20", "8", "6"],
        ["004243", "C", "2022-12-19", "", "12"],
        ["519212", "B", "2022-12-18", "56", "3"],
        ["162411", "E", "2022-12-17", "8", "7"],
        ["007844", "FC", "2022-12-16", "", "8"],
        ["163208", "G", "2022-12-15", "66", "12"],
        ["162719", "A", "2022-12-14", "8", "6"],
        ["519212", "B", "2022-12-12", "56", "3"],
        ["162411", "E", "2022-12-11", "8", "7"],
        ["007844", "FC", "2022-12-10", "", "18"],
        ["163208", "G", "2022-12-8", "66", "12"]]

# 定义列名
columns = ["编号", "名称", "日期", "A", "B"]


# 生成数据帧

df = pd.DataFrame(data, columns=columns)


# 将“日期”列转换为datetime类型
df['日期'] = pd.to_datetime(df['日期'], errors='coerce')


# 根据日期排序,并以日期作为索引
df = df.sort_values(by='日期').set_index('日期')


# 根据指定范围选择行
selected_rows
有用请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-17 17:42:48 | 显示全部楼层
isdkz 发表于 2023-4-17 17:27
为了找到给定时间段内的B列的最大值及对应的日期,我们需要首先筛选出符合条件的行,然后找到最大值以及对 ...

日期序列是这样的,大师你取它的[-1],取到了正确结果,能解释下吗?实在是不理解
2   2022-12-19
6   2022-12-15
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-15 04:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表