1

说,我们建立一个df:

import pandas as pd
import random as randy
import numpy as np
df_size = int(1e6)
df = pd.DataFrame({'first':       randy.sample(np.repeat([np.NaN,'Cat','Dog','Bear','Fish'],df_size),df_size),
               'second': randy.sample(np.repeat([np.NaN,np.NaN,'Cat','Dog'],df_size),df_size),
                'value': range(df_size)},
                index=randy.sample(pd.date_range('2013-02-01 09:00:00.000000',periods=1e6,freq='U'),df_size)).sort_index()

它看起来像这样:

                            first   second    value
2013-02-01 09:00:00          Fish    Cat     95409
2013-02-01 09:00:00.000001   Dog     Dog     323089
2013-02-01 09:00:00.000002   Fish    Cat     785925
2013-02-01 09:00:00.000003   Dog     Cat     866171
2013-02-01 09:00:00.000004   nan     nan     665702
2013-02-01 09:00:00.000005   Cat     nan     104257
2013-02-01 09:00:00.000006   nan     nan     152926
2013-02-01 09:00:00.000007   Bear    Cat     707747

我想要的是“第二”列中的每个值,我想要第一个的最后一个“值”。

                            first   second   value  new_value
2013-02-01 09:00:00         Fish     Cat     95409    NaN
2013-02-01 09:00:00.000001   Dog     Dog     323089   323089
2013-02-01 09:00:00.000002   Fish    Cat     785925   NaN
2013-02-01 09:00:00.000003   Dog     Cat     866171   NaN
2013-02-01 09:00:00.000004   nan     nan     665702   NaN
2013-02-01 09:00:00.000005   Cat     nan     104257   NaN
2013-02-01 09:00:00.000006   nan     nan     152926   NaN
2013-02-01 09:00:00.000007   Bear    Cat     707747   104257

也许,这不是绝对最好的例子,但在底部,当“第二”是“猫”时,我想要“第一”是“猫”时的最新值

真实的数据集有 1000 多个类别,因此遍历符号并执行 asof() 似乎非常昂贵。我从来没有在 Cython 中传递字符串的运气,但我想将符号映射到整数并做一个蛮力循环会起作用——我希望有更多的 Pythonic。(这仍然相当快)

一个参考,并且有些脆弱的 Cython hack 将是:

%%cython
import numpy as np
import sys
cimport cython
cimport numpy as np

ctypedef np.double_t DTYPE_t

def last_of(np.ndarray[DTYPE_t, ndim=1] some_values,np.ndarray[long, ndim=1] first_sym,np.ndarray[long, ndim=1] second_sym):
    cdef long val_len = some_values.shape[0], sym1_len = first_sym.shape[0], sym2_len = second_sym.shape[0], i = 0
    assert(sym1_len==sym2_len)
    assert(val_len==sym1_len)
    cdef int enum_space_size = max(first_sym)+1

    cdef np.ndarray[DTYPE_t, ndim=1] last_values = np.zeros(enum_space_size, dtype=np.double) * np.NaN
    cdef np.ndarray[DTYPE_t, ndim=1] res = np.zeros(val_len, dtype=np.double) * np.NaN
    for i in range(0,val_len):
        if first_sym[i]>=0:
            last_values[first_sym[i]] = some_values[i]
        if second_sym[i]<0 or second_sym[i]>=enum_space_size:
            res[i] = np.NaN
        else:
            res[i] = last_values[second_sym[i]]
    return res

然后一些 dict 代替废话:

syms= unique(df['first'].values)
enum_dict = dict(zip(syms,range(0,len(syms))))
enum_dict['nan'] = -1
df['enum_first'] = df['first'].replace(enum_dict)
df['enum_second'] = df['second'].replace(enum_dict)
df['last_value'] = last_of(df.value.values*1.0,df.enum_first.values.astype(int64),df.enum_second.values.astype(int64))

这有一个问题,如果“第二”列有任何不在第一列的值,那么你就有问题了。(我不确定解决这个问题的快速方法......假设你在第二个中添加了“驴”)

每 1000 万行的 cythonic 愚蠢版本对于整个混乱来说是 ~ 21 秒,但对于 cython 部分只有 ~2 秒。(这可以做得更快)

@HYRY——我认为这是一个非常可靠的解决方案;在具有 1000 万行的 DF 上,在我的笔记本电脑上,这对我来说大约需要 30 秒。

鉴于我不知道当第二个列表除了非常昂贵的 isin 之外没有第一个条目时的简单处理方法,我认为 HYRY 的 python 版本非常好。

4

2 回答 2

3

如何使用 dict 保留每个类别的最后一个值,并迭代 DataFrame 中的所有行:

import pandas as pd
import random as randy
import numpy as np
np.random.seed(1)
df_size = int(1e2)
df = pd.DataFrame({'first':       randy.sample(np.repeat([None,'Cat','Dog','Bear','Fish'],df_size),df_size),
               'second': randy.sample(np.repeat([None,None,'Cat','Dog'],df_size),df_size),
                'value': range(df_size)},
                index=randy.sample(pd.date_range('2013-02-01 09:00:00.000000',periods=1e6,freq='U'),df_size)).sort_index()

last_values = {}
new_values = []
for row in df.itertuples():
    t, f, s, v = row    
    last_values[f] = v
    if s is None:
        new_values.append(None)
    else:
        new_values.append(last_values.get(s, None))
df["new_value"] = new_values

结果是

                          first second  value new_value
2013-02-01 09:00:00.010373   Cat   None     87      None
2013-02-01 09:00:00.013015   Cat    Dog     69      None
2013-02-01 09:00:00.024910  Fish    Cat      1        69
2013-02-01 09:00:00.025943   Cat   None     98      None
2013-02-01 09:00:00.041318  Fish    Dog     66      None
2013-02-01 09:00:00.057894  None   None     36      None
2013-02-01 09:00:00.059678  None   None     50      None
2013-02-01 09:00:00.067228  Bear   None     38      None
2013-02-01 09:00:00.095867  Bear    Cat     84        98
2013-02-01 09:00:00.096867   Dog    Cat     97        98
2013-02-01 09:00:00.101540   Dog    Dog     76        76
2013-02-01 09:00:00.106753   Dog   None     22      None
2013-02-01 09:00:00.138936  None   None      8      None
2013-02-01 09:00:00.139273  Bear    Cat      2        98
2013-02-01 09:00:00.143180  Fish   None     94      None
2013-02-01 09:00:00.184757  None    Cat     73        98
2013-02-01 09:00:00.193063  None   None      5      None
2013-02-01 09:00:00.231056  Fish    Cat     62        98
2013-02-01 09:00:00.237658  None   None     64      None
2013-02-01 09:00:00.240178  Bear    Dog     80        22
于 2013-02-24T05:49:00.373 回答
0

我知道的老问题,但这是一个避免任何 Python 循环的解决方案。第一步是获取'value'每个类别的时间序列。您可以通过取消堆叠来做到这一点:

first_values = df.dropna(subset=['first']).set_index('first', append=True).value.unstack()    
second_values = df.dropna(subset=['second']).set_index('second', append=True).value.unstack()

请注意,这仅在列包含真NaN值而不是'nan'字符串时才有效(df = df.replace('nan', np.nan)如有必要,请做好准备)。

然后,您可以通过前向填充first_values、重新索引 like 、再次堆叠并使用原始对second_values索引到结果中来获得最后一个值:'time', 'second'

ix = pd.MultiIndex.from_arrays([df.index, df.second])
new_value = first_values.ffill().reindex_like(second_values).stack().reindex(ix)
df['new_value'] = new_value.values

In [1649]:    df
Out[1649]:
                           first    second  value   new_value
2013-02-01 09:00:00.000000  Fish    Cat     95409   NaN
2013-02-01 09:00:00.000001  Dog     Dog     323089  323089
2013-02-01 09:00:00.000002  Fish    Cat     785925  NaN
2013-02-01 09:00:00.000003  Dog     Cat     866171  NaN
2013-02-01 09:00:00.000004  NaN     NaN     665702  NaN
2013-02-01 09:00:00.000005  Cat     NaN     104257  NaN
2013-02-01 09:00:00.000006  NaN     NaN     152926  NaN
2013-02-01 09:00:00.000007  Bear    Cat     707747  104257
于 2015-10-15T09:29:09.883 回答