如何获取 pandas 数据框作为一个系列?
How to get pandas dataframe as a series?
我正在尝试 python.I 的 statsmodels
库有一个数据具有某些 trend
和 seasonality
。statsmodels
提供了一种绘制方法这些趋势并获得坐标 dataframe.This 是我下面的代码
df = pd.read_csv("test_forecast/upload_data.csv")
df["date"] = pd.to_datetime(df["date"], format="%Y-%m-%d")
df.set_index("date", inplace=True)
df = df.resample('D').mean().interpolate(method='linear', axis=0).fillna(0)
现在我尝试获取 trend
.
的坐标
print(sm.tsa.seasonal_decompose(df["upload"]).trend.interpolate(method='linear', axis=0).fillna(0))
这是输出
date
2018-04-22 3.919738
2018-04-23 3.985145
2018-04-24 3.839589
2018-04-25 3.723810
2018-04-26 3.566047
2018-04-27 3.416895
2018-04-28 3.215901
... ...
... ...
现在我想要 list.So 形式的输出 我用 .values
执行代码
print(sm.tsa.seasonal_decompose(df["upload"]).trend.interpolate(method='linear', axis=0).fillna(0).values)
我得到如下输出
[3.91973791 3.98514482 3.83958857 3.72381001 3.56604662
3.41689526 3.21590053 3.21826295 3.17641971 3.25942285 3.39823427
3.51068301 3.80029493 4.17883987 4.40204831]
但我也想要 date
值,以便我的输出类似于
[['2018-04-22', 3.919738], ['2018-04-23', 3.985145], ['2018-04-24', 3.839589]].....
如何在数组中获取 date
以及值?
尝试,
df.reset_index().values.tolist()
除了 @pyd
的答案之外,获得所需输出的完整答案(没有 reindexing
并使用 tolist()
)是
coordinates = list(map(list, zip(df.index.strftime('%Y-%m-%d'), sm.tsa.seasonal_decompose(df["upload"]).trend.interpolate(method='linear', axis=0).fillna(0).values)))
我正在尝试 python.I 的 statsmodels
库有一个数据具有某些 trend
和 seasonality
。statsmodels
提供了一种绘制方法这些趋势并获得坐标 dataframe.This 是我下面的代码
df = pd.read_csv("test_forecast/upload_data.csv")
df["date"] = pd.to_datetime(df["date"], format="%Y-%m-%d")
df.set_index("date", inplace=True)
df = df.resample('D').mean().interpolate(method='linear', axis=0).fillna(0)
现在我尝试获取 trend
.
print(sm.tsa.seasonal_decompose(df["upload"]).trend.interpolate(method='linear', axis=0).fillna(0))
这是输出
date
2018-04-22 3.919738
2018-04-23 3.985145
2018-04-24 3.839589
2018-04-25 3.723810
2018-04-26 3.566047
2018-04-27 3.416895
2018-04-28 3.215901
... ...
... ...
现在我想要 list.So 形式的输出 我用 .values
print(sm.tsa.seasonal_decompose(df["upload"]).trend.interpolate(method='linear', axis=0).fillna(0).values)
我得到如下输出
[3.91973791 3.98514482 3.83958857 3.72381001 3.56604662
3.41689526 3.21590053 3.21826295 3.17641971 3.25942285 3.39823427
3.51068301 3.80029493 4.17883987 4.40204831]
但我也想要 date
值,以便我的输出类似于
[['2018-04-22', 3.919738], ['2018-04-23', 3.985145], ['2018-04-24', 3.839589]].....
如何在数组中获取 date
以及值?
尝试,
df.reset_index().values.tolist()
除了 @pyd
的答案之外,获得所需输出的完整答案(没有 reindexing
并使用 tolist()
)是
coordinates = list(map(list, zip(df.index.strftime('%Y-%m-%d'), sm.tsa.seasonal_decompose(df["upload"]).trend.interpolate(method='linear', axis=0).fillna(0).values)))