PANDA中的Groupby算法和插值算法

2024-08-22Python开发问题
11

vpn free 本文介绍了PANDA中的Groupby算法和插值算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧! 免费vpn 免费vpn free vpn vpn free 免费vpn下载

问题描述

免费vpn 我有一个数据,其中包含周号、帐户ID和几个使用情况列。我希望a)按帐户ID分组,b)将每周数据重新采样为每日数据,以及c)均匀内插每日数据(将每周数据除以7),然后将其全部组合在一起。我已经记下了大部分,但是 免费vpn pandas groupby让我有点迷惑。它也非常慢,这让我认为这可能不是最佳解决方案。

数据如下:

 vpn free  免费vpn  免费vpn vpn下载  免费vpn下载 vpn下载 Account Id  year week         views stats foo_col 
31133   213    免费vpn  2017-03-05        vpn下载   4.0     vpn free 2.0     11.0
10085   456     2017-03-12 free vpn          1.0     6.0     vpn free 3.0
49551   789     2017-03-26          1.0     6.0     27.0

以下是我的代码:

def interpolator(mini_df):
    mini_df = mini_df[cols_to_interpolate].set_index('year week')
 free vpn    return mini_df.resample('D').ffill().interpolate() / 7

example = list(grp)[0][1]
interpolator(example) # vpn下载 This works perfectly

df.groupby('Account Id').agg(interpolator) vpn free  免费vpn下载      free vpn          # doesn't work
df.groupby('Account Id').transform(interpolator)          # doesn't work

for name,group in grp:
    group = group[cols_to_interpolate].set_index('year week')
    group = group.resample('D').ffill().interpolate() / 7 # doesn't work

for acc_id in df['Account Id'].unique():
    mask = df.loc[df['Account Id'] == acc_id]
    print(df[mask])           免费vpn         vpn下载           免费vpn         # doesn't work

推荐答案

我希望您的函数应该与groupby对象链接在一起,如下所示:

df vpn下载 = (df.set_index('year week')
        .groupby('Account Id')[cols_to_interpolate]
  vpn下载  free vpn      .resample('D')
      vpn下载   .ffill()
    免费vpn下载    免费vpn  .interpolate() / 7)

注释中的解决方案不同-interpolate适用于每个组:

df.groupby('Account Id').apply(interpolator)

这篇关于PANDA中的Groupby算法和插值算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas 免费vpn without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗? vpn下载
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab 免费vpn下载 value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
vpn free 2024-08-22 免费vpn下载 Python开发问题
18

PANDA中的Groupby算法和插值算法 vpn free
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a vpn free column and replace NaN with non-null vpn free vpn下载 values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10