python - 如何从时间序列数据中选择前 n 列而不是在 pandas 中使用 nlargest

我有每周的贸易导出时间序列数据,我需要制作堆积条形图来可视化贸易事件。为此,我汇总了所有行的每一列的总和数据,然后使用 nlargest() 选择前 n 列。但是,这样做可能不太准确,因为我在循环中为不同年份制作了堆叠图,并且每年的前 n 列可能不同。但是我所做的是,对所有行(又名,包括所有年份)求出每列的总和,然后选择前 n 列,这是有偏差的。所以,我正在研究不同的方法,也许我可以按每年对时间序列数据进行分组,然后绘制堆积图。有没有其他方法可以从时间序列数据中选择前 n 列而不是使用 nlargest?有谁知道这样做的任何可能方法?我们还有什么其他方法可以从时间序列数据中选择前 n 列?有什么想法吗?

我目前的尝试:

这是我目前对操作时间序列数据的尝试,我在其中聚合所有行的每一列,然后使用 nlargest() 选择前 n 列:

import pandas as pd

# load the data
url = 'https://gist.githubusercontent.com/adamFlyn/a6048e547b5a963c7af356c964d15af6/raw/c57c7915cf14f81edc9d5eadaf14efbd43d3e58a/trade_df.csv'
df_ = pd.read_csv(url, parse_dates=['weekly'])
df_.set_index('weekly', inplace=True)
df_.loc['Total',:]= df_.sum(axis=0)
df1 = df_.T
df1 =df1.nlargest(6, columns=['Total'])
df1.drop('Total', axis=1, inplace=True)
df2 = df1.T
df2.reset_index(inplace=True)
df2['weekly'] = pd.to_datetime(df2['weekly'])
df2['year'] = df2['weekly'].dt.year
df2['week'] = df2['weekly'].dt.strftime('%W').astype('int')

然后我使用 matplotlib 将绘图数据可视化如下:

import matplotlib.pyplot as plt

plt_df = df2.set_index(['year','week'])
plt_df.drop("weekly", axis=1, inplace=True)
for n, g in plt_df.groupby(level=0):
    ax = g.loc[n].plot.bar(stacked=True, title=f'{n} Year', figsize=(8,5))
    plt.show()

尽管output of current approach in stacked plot 很好,但是使用 nlargest() 选择前 n 列不太准确。例如,在 2019 年美国农业部报告中,中国不是美国的最大贸易伙伴,但在 2020 年底,中国是从美国获得更多产品,如果我使用 nlargest() 选择顶部列(或贸易伙伴),这将有问题,中国不会出现在列表中,也不会出现在图中。

更新

正如@Vaishali 在评论中建议的 this post ,使用 head() 可能是提取顶部列的好主意,所以我这样尝试:

for n, g in plt_df.groupby(level=0):
    for i in g:
        gg = g[i].sort_values(g[i].values,ascending = False).groupby('week').head(5)
        ax = gg.loc[n].plot.bar(stacked=True, title=f'{n} Year', figsize=(8,5))

但这行不通。谁能指出如何从时间序列数据中选择前 n 列?有什么想法吗?

最佳答案

你可以尝试这样的事情:

url = 'https://gist.githubusercontent.com/adamFlyn/a6048e547b5a963c7af356c964d15af6/raw/c57c7915cf14f81edc9d5eadaf14efbd43d3e58a/trade_df.csv'
df_ = pd.read_csv(url, parse_dates=['weekly'])
df_['weekly'] = pd.to_datetime(df_['weekly'])
df_.set_index('weekly', inplace=True)

for g, n in df_.groupby(df_.index.year):
    ng = n.loc[:, n.sum().rank(ascending=False, method='min')<5]
    ng.div(ng.sum(axis=1), axis=0).plot.area(title=f'{g}')

输出:

条形图:

将 matplotlib.ticker 导入为 mticker

url = 'https://gist.githubusercontent.com/adamFlyn/a6048e547b5a963c7af356c964d15af6/raw/c57c7915cf14f81edc9d5eadaf14efbd43d3e58a/trade_df.csv'
df_ = pd.read_csv(url, parse_dates=['weekly'])
df_['weekly'] = pd.to_datetime(df_['weekly'])
df_.set_index('weekly', inplace=True)

for g, n in df_.groupby(df_.index.year):
    ng = n.loc[:, n.sum().rank(ascending=False, method='min')<5]
    ng.index = ng.index.strftime('%m/%d/%Y')
    ax = ng.plot.bar(stacked=True, figsize=(10,8))

输出:

质押 100% 条形图:

#(previous code)
ax = ng.div(ng.sum(axis=1), axis=0).plot.bar(stacked=True, figsize=(10,8))

输出:

关于python - 如何从时间序列数据中选择前 n 列而不是在 pandas 中使用 nlargest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65907197/

相关文章:

javascript - 对象可能是 'null' : TypeScript, React useR

vuejs3 - vue3 中 shallowReactive 和 shallowRef 的区别?

react-native - react native : Refresh Controll not

c# - 如何使 IEnumerable 和 IQueryable 异步?

javascript - 句号、单词和冒号的正则表达式

python - 如何连接来自 3 个小整数的字节以生成由 Python 中的这些字节表示的更大数字

reactjs - 有没有 Ionicons : Icon props?

list - 在 Elixir 中使用类型规范中的原子列表

javascript - Axios 响应未定义

haskell - 为什么 map 使用的这个函数需要返回一个(单例)列表而不是一个元素?