Pandas数据帧定制前向填充优化

2024-03-28 15:27:54 发布

您现在位置:Python中文网/ 问答频道 /正文

问题

亲爱的各位,
我有一个函数,可以根据dataframe的一些规范列进行同步。
功能正常,但我想知道如何操作:

  • 提高绩效
  • 让它更像Python

请随时提出建议,
谢谢。你知道吗


函数、样本和结果

功能规格

  1. 输入:

    • df:带列的dataframe
      • [a0,...aN]a0aN名称可以是任何有效的string并包含numeric
      • [agent,date]:是固定名称,agent包含numeric值,date包含datetime。你知道吗
    • sync_with:要与[a0,..., aN]中包含的stringlistof string同步的列,或者默认情况下,与empty list同步所有[a0,...,aN]的列。你知道吗
  2. 同步:

    • 执行按代理值分组的前向fillna。你知道吗
    • 删除要与值同步的所有列都为空的行
  3. 返回:同步的dataframe

我的职责是:

import pandas as pd
import numpy as np

def synchronize(df,sync_with=[]):
    _df = df.copy()

    if not isinstance(sync_with,list):
        sync_with = [sync_with]

    _fixed_cols = ['date','agent']
    _fixed_cols.extend(sync_with)
    _colset = [c for c in _df.columns if c not in _fixed_cols]

    for ag in _df.agent.unique():
        _df.loc[_df.agent==ag,_colset] = _df.loc[_df.agent==ag,_colset].fillna(method='ffill')
        if sync_with:
            _df = _df.dropna(how='all', subset=sync_with)
            _df.loc[_df.agent==ag,:] = _df.loc[_df.agent==ag,:].fillna(method='ffill')

    return _df

样品

foo = pd.DataFrame(dict(date=pd.to_datetime(['2010', '2011', '2012', '2013', '2010', '2013', '2015', '2016']),
                        agent=[1,1,1,1,2,2,2,2],
                        _a=[1, np.nan, np.nan, 4, 5, np.nan, 7, 8],
                        _b=[11, 22, np.nan, np.nan, 55, np.nan, 77, np.nan],
                        _c=[111, np.nan, 333, np.nan, np.nan, 666, 777, np.nan]))

结果

# 1. default (10.1 ms per loop)
print(synchronize(foo))
    _a    _b     _c  agent       date
0  1.0  11.0  111.0      1 2010-01-01
1  1.0  22.0  111.0      1 2011-01-01
2  1.0  22.0  333.0      1 2012-01-01
3  4.0  22.0  333.0      1 2013-01-01
4  5.0  55.0    NaN      2 2010-01-01
5  5.0  55.0  666.0      2 2013-01-01
6  7.0  77.0  777.0      2 2015-01-01
7  8.0  77.0  777.0      2 2016-01-01

# 2. sync with one column (54.9 ms per loop)
print(synchronize(foo,'_c'))
    _a    _b     _c  agent       date
0  1.0  11.0  111.0      1 2010-01-01
2  1.0  22.0  333.0      1 2012-01-01
5  NaN   NaN  666.0      2 2013-01-01
6  7.0  77.0  777.0      2 2015-01-01

# 3. sync with two columns (53.4 ms per loop)
print(synchronize(foo,['_a','_b'))
    _a    _b     _c  agent       date
0  1.0  11.0  111.0      1 2010-01-01
1  1.0  22.0  111.0      1 2011-01-01
3  4.0  22.0  333.0      1 2013-01-01
4  5.0  55.0    NaN      2 2010-01-01
6  7.0  77.0  777.0      2 2015-01-01
7  8.0  77.0  777.0      2 2016-01-01

Tags: andataframedfsynchronizedatefoowithnp