从熊猫框架中的一系列数据中找出值和日期
Posted
技术标签:
【中文标题】从熊猫框架中的一系列数据中找出值和日期【英文标题】:Find out the values and dates from series of data in pandas frame 【发布时间】:2021-05-28 12:59:28 【问题描述】:我正在用 python pandas 编写一个脚本,我必须在其中找到值和日期的第一个下降点,然后在哪里达到最大值,然后再下降值和日期。然后又是下降点值和日期。在下面显示的图表中,我标记了我想要从中获取日期和值的红色圆圈。我有一个脚本,但我需要提及获取值的日期,但我想提取日期和值,任何帮助将不胜感激。
代码:
import pandas as pd
df = pd.read_csv(r"D:\Data\2015_20.csv", parse_dates=["Date"])
df = df[["Date", "Mean"]]
df = df.set_index("Date")
z1 = df['2016-04-28' : '2017-02-22'].min()
z2 = df['2017-05-13' : '2018-02-02'].max()
z3 = df['2018-03-19' : '2019-03-04'].max()
print("2016", '%.2f'%z1)
print("2017", '%.2f'%z2)
print("2018", '%.2f'%z3)
【问题讨论】:
只要写df['particular date']
就会得到对应的mean
值
我不想写日期,我希望脚本本身会打印日期和值。
【参考方案1】:
您可以使用argrelextrema
查找本地最小值和最大值:
from scipy.signal import argrelextrema
np.random.seed(0)
rs = np.random.randn(200)
xs = [0]
for r in rs:
xs.append(xs[-1] * 0.9 + r)
df = pd.DataFrame(xs, columns=['data'], index=pd.date_range('2000-01-01',periods=len(xs)))
n = 5 # number of points to be checked before and after
# Find local peaks
df['min'] = df.iloc[argrelextrema(df.data.values, np.less_equal,
order=n)[0]]['data']
df['max'] = df.iloc[argrelextrema(df.data.values, np.greater_equal,
order=n)[0]]['data']
df['min_date'] = df.index.where(df['min'].notna())
df['max_date'] = df.index.where(df['max'].notna())
print (df.head(15))
data min max min_date max_date
2000-01-01 0.000000 0.000000 NaN 2000-01-01 NaT
2000-01-02 1.764052 NaN NaN NaT NaT
2000-01-03 1.987804 NaN NaN NaT NaT
2000-01-04 2.767762 NaN NaN NaT NaT
2000-01-05 4.731879 NaN NaN NaT NaT
2000-01-06 6.126249 NaN 6.126249 NaT 2000-01-06
2000-01-07 4.536346 NaN NaN NaT NaT
2000-01-08 5.032800 NaN NaN NaT NaT
2000-01-09 4.378163 NaN NaN NaT NaT
2000-01-10 3.837128 NaN NaN NaT NaT
2000-01-11 3.864013 NaN NaN NaT NaT
2000-01-12 3.621656 3.621656 NaN 2000-01-12 NaT
2000-01-13 4.713764 NaN NaN NaT NaT
2000-01-14 5.003425 NaN NaN NaT NaT
2000-01-15 4.624757 NaN NaN NaT NaT
编辑:
来自真实数据的解决方案:
df['Date'] = pd.to_datetime(df['Date'])
df = df.set_index('Date')
from scipy.signal import argrelextrema
n = 5
s1 = df.iloc[argrelextrema(df.Mean.values, np.less_equal,
order=n)[0]]['Mean']
s2 = df.iloc[argrelextrema(df.Mean.values, np.greater_equal,
order=n)[0]]['Mean']
s = s1.append(s2).sort_index()
print (s)
Date
2016-05-18 0.293171
2016-11-04 0.692509
2017-05-13 0.232963
2017-09-10 0.675797
2017-11-09 0.528592
2018-04-03 0.189523
2018-11-09 0.713351
Name: Mean, dtype: float64
s.to_csv('out.csc')
【讨论】:
@jezreal,正如我在帖子中提到的那样,我正在处理 CSV 数据。如何在此处提供 CSV 作为输入。 @user286076 - 使用你的解决方案df = pd.read_csv(r"D:\Data\2015_20.csv", parse_dates=["Date"]) df = df[["Date", "Mean"]] df = df.set_index("Date")
,有问题吗?
我在几个地方遇到了日期错误,请您根据我在上面发布的输入数据设计代码。谢谢
@user286076 - 什么是错误? Please don't post images of code/data (or links to them)
@user286076 - 样本数据的预期输出是什么?新的 DataFrame 看起来如何?以上是关于从熊猫框架中的一系列数据中找出值和日期的主要内容,如果未能解决你的问题,请参考以下文章