使用
pandas.rolling_corr,而不是
Dataframe.rolling_corr。此外,还
groupby返回一个生成器。参见下面的代码。
码:
import pandas as pddf = pd.read_csv("color.csv")df_gen = df.copy().groupby("Color")for key, value in df_gen: print "key: {}".format(key) print value.rolling_corr(value["Value1"],value["Value2"], 3)
输出:
key: Blue1 NaN3 NaN6 0.9316738 0.86506610 0.08930412 -0.99865615 -0.97137317 -0.667316dtype: float64key: Red0 NaN2 NaN5 -0.9113579 -0.15222111 -0.97115314 0.43869718 -0.550727dtype: float64key: Yellow4 NaN7 NaN13 -0.04033016 0.879371dtype: float64
您可以将循环部分更改为以下内容,以查看原始数据帧的后分组以及新列。
for key, value in df_gen: value["ROLL_CORR"] = pd.rolling_corr(value["Value1"],value["Value2"], 3) print value
输出:
Color Value1 Value2 ROLL_CORR1 Blue 0.951227 0.514999 NaN3 Blue 0.649112 0.513052 NaN6 Blue 0.148165 0.342205 0.9316738 Blue 0.626883 0.421530 0.86506610 Blue 0.286738 0.583811 0.08930412 Blue 0.966779 0.227340 -0.99865615 Blue 0.065493 0.887640 -0.97137317 Blue 0.757932 0.900103 -0.667316key: Red Color Value1 Value2 ROLL_CORR0 Red 0.201435 0.981871 NaN2 Red 0.522955 0.357239 NaN5 Red 0.806326 0.310039 -0.9113579 Red 0.656126 0.678047 -0.15222111 Red 0.435898 0.908388 -0.97115314 Red 0.116419 0.555821 0.43869718 Red 0.793102 0.168033 -0.550727key: Yellow Color Value1 Value2 ROLL_CORR4 Yellow 0.099474 0.143293 NaN7 Yellow 0.073128 0.749297 NaN13 Yellow 0.006777 0.318383 -0.04033016 Yellow 0.345647 0.993382 0.879371
如果要在处理后将它们全部结合在一起(顺便说一句,这可能会使其他人感到困惑),请
concat在处理组之后使用。
import pandas as pddf = pd.read_csv("color.csv")df_gen = df.copy().groupby("Color")dfs = [] # Container for dataframes.for key, value in df_gen: value["ROLL_CORR"] = pd.rolling_corr(value["Value1"],value["Value2"], 3) print value dfs.append(value)df_final = pd.concat(dfs)print df_final
输出:
Color Value1 Value2 ROLL_CORR1 Blue 0.951227 0.514999 NaN3 Blue 0.649112 0.513052 NaN6 Blue 0.148165 0.342205 0.9316738 Blue 0.626883 0.421530 0.86506610 Blue 0.286738 0.583811 0.08930412 Blue 0.966779 0.227340 -0.99865615 Blue 0.065493 0.887640 -0.97137317 Blue 0.757932 0.900103 -0.6673160 Red 0.201435 0.981871 NaN2 Red 0.522955 0.357239 NaN5 Red 0.806326 0.310039 -0.9113579 Red 0.656126 0.678047 -0.15222111 Red 0.435898 0.908388 -0.97115314 Red 0.116419 0.555821 0.43869718 Red 0.793102 0.168033 -0.5507274 Yellow 0.099474 0.143293 NaN7 Yellow 0.073128 0.749297 NaN13 Yellow 0.006777 0.318383 -0.04033016 Yellow 0.345647 0.993382 0.879371
希望这可以帮助。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)