分组和转换熊猫数据

时间:2013-10-08 10:48:02

标签: python pandas dataframe

我有一个时间序列数据集,有10个单位的观察。每天观察的每个单元都可以进行数小时的活动。因此,我(比方说)在DataFrame中有4个变量:

日期 - 日期 单位 - 观察单位 活动 - 一些活动 CumHours - 单位在活动上累计花费的小时数。

我需要创建一个'Hours_calc'变量,该变量表示每天每行的任何活动花费了多少小时。在导入所有正常位后,我执行此操作:

DF = (mydata, columns = ['Date', 'Unit', 'Activity', 'CumHours'])
DF['Hours_calc'] = np.nan
DFGrouped = DF.groupby(['Unit', 'Activity'])

我相信我需要变换方法,我认为传递给transform函数(如指定的)的第一个参数是DF列,与agg()方法一样。是对的吗?

我编写了以下函数,但是你会看到我不知道我是否应该参考DF或DFGrouped进行计算,或者是什么?一般来说如何引用goruped数据中的行/列?另外,我是否正确地尝试返回与groupby组相同长度的系列?

def WorkingHours(x, Column):
    LISTHOURS = []
    for row in xrange(1, len(x)):
        Tot = int(DFGrouped[Column][row]) - int(DFGrouped[Column][row - 1])
        LISTHOURS.append(Tot)
    return pd.Series(LISTHOURS)

TESTDF = DFGrouped.transform({Hours_calc : lambda x : WorkingHours(x, 'CumHours')})

此外,我没有在文档中看到有关将Dict传递给转换的任何内容,但我看不到在gorup中只对一个变量/列执行该函数的任何其他方法。

我哪里错了?错误消息长期令人困惑,但以“转换函数对数据类型无效”

结束

2 个答案:

答案 0 :(得分:2)

我认为你可以使用shift函数将分组数据中的列移动一行。然后,如果您减去原始数据并进行移位,您将获得在活动上花费的原始小时数。

df = (mydata, columns = ['date', 'unit', 'activity', 'cumhours'])
grouped = df.groupby(['unit', 'activity'])
shift_it = lambda x: (x - x.shift())
raw_hours = grouped.cumhours.transform(shift_it)
df.insert(0, 'raw_hours',raw_hours)

编辑1。 如果您希望通用函数使用transform迭代系列元素,但不复制到列表,请尝试:

df = (mydata, columns = ['date', 'unit', 'activity', 'cumhours'])
grouped = df.groupby(['unit', 'activity'])
def trans_func(x):
    y=x.copy()
    for i in range(1,len(x.index)):
        x.iloc[i]=y.iloc[i]-y.iloc[i-1]
    return x

raw_hours = gr['cumhours'].transform(lambda x: trans_func(x))
df.insert(0, 'raw_hours',raw_hours)

答案 1 :(得分:1)

我想我终于解决了这个问题。我怀疑yemu的答案是好的,但我更喜欢这个,因为这是一个最终学习如何应用我自己的功能的练习:

DF = (mydata, columns = ['Date', 'Unit', 'Activity', 'CumHours'])
DF['Hours_calc'] = np.nan
DFGrouped = DF.groupby(['Unit', 'Activity'])

def WorkingHours(x):
    CumHoursList = list(x)
    HoursCalcList = []
    HoursCalcList.insert(0, CumHoursList[0])
    for index in xrange(1, len(CumHoursList)):
        CalcHoursList/insert(index, CumHoursList[index] - CumHoursList[index - 1])
    return CalcHoursList

DFGrouped['CumHours'].transform(lambda x: WorkingHours(x))

有魅力!