运行func(df)以创建新的数据帧并重命名它们

时间:2016-12-06 13:24:45

标签: python pandas

我可以保留df10&的名称吗? df20相同并在运行func(df)后单独调用它们,甚至重命名它们?

df = pd.DataFrame( {
   'A': ['d','d','d','d','d','d','g','g','g','g','g','g','k','k','k','k','k','k'],
   'B': [5,5,6,4,5,6,-6,7,7,6,-7,7,-8,7,-6,6,-7,50],
   'C': [1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2],
   'S': [2012,2013,2014,2015,2016,2012,2012,2014,2015,2016,2012,2013,2012,2013,2014,2015,2016,2014]     
    } );

df10 = (df.B + df.C).groupby([df.A, df.S]).agg(['sum','size']).unstack(fill_value=0)

df20 = (df['B'] - df['C']).groupby([df.A, df.S]).agg(['sum','size']).unstack(fill_value=0)

def func(df):
    df1 = df.groupby(level=0, axis=1).sum()
    new_cols= list(zip(df1.columns.get_level_values(0),['total'] * len(df.columns)))
    df1.columns = pd.MultiIndex.from_tuples(new_cols)
    df2 = pd.concat([df1,df], axis=1).sort_index(axis=1).sort_index(axis=1, level=1)
    df2.columns = ['_'.join((col[0], str(col[1]))) for col in df2.columns]
    df2.columns = df2.columns.str.replace('sum_','')
    df2.columns = df2.columns.str.replace('size_','T')
    return df2

dfs = [] 
for df in [df10, df20]: 
    dfs.append(func(df))

dfs

1 个答案:

答案 0 :(得分:1)

您可以使用DataFrames存储和列表来创建dfs中存储的names = ['a','b'] dfs = {names[i]:func(df) for i,df in enumerate([df10, df20])} print (dfs) {'a': T2012 2012 T2013 2013 T2014 2014 T2015 2015 T2016 2016 Ttotal \ A d 2 13 1 6 1 7 1 5 1 6 6 g 2 -11 1 8 1 8 1 8 1 7 6 k 1 -6 1 9 2 48 1 8 1 -5 6 total A d 37 g 20 k 54 , 'b': T2012 2012 T2013 2013 T2014 2014 T2015 2015 T2016 2016 Ttotal \ A d 2 9 1 4 1 5 1 3 1 4 6 g 2 -15 1 6 1 6 1 6 1 5 6 k 1 -10 1 5 2 40 1 4 1 -9 6 total A d 25 g 8 k 30 } 新名称:

print (dfs['a'])
   T2012  2012  T2013  2013  T2014  2014  T2015  2015  T2016  2016  Ttotal  \
A                                                                            
d      2    13      1     6      1     7      1     5      1     6       6   
g      2   -11      1     8      1     8      1     8      1     7       6   
k      1    -6      1     9      2    48      1     8      1    -5       6   

   total  
A         
d     37  
g     20  
k     54  
print (dfs['b'])
   T2012  2012  T2013  2013  T2014  2014  T2015  2015  T2016  2016  Ttotal  \
A                                                                            
d      2     9      1     4      1     5      1     3      1     4       6   
g      2   -15      1     6      1     6      1     6      1     5       6   
k      1   -10      1     5      2    40      1     4      1    -9       6   

   total  
A         
d     25  
g      8  
k     30  
DataFrames

但如果需要func的相同名称,您可以将函数df10 = func(df10) df20 = func(df20) print (df10) T2012 2012 T2013 2013 T2014 2014 T2015 2015 T2016 2016 Ttotal \ A d 2 13 1 6 1 7 1 5 1 6 6 g 2 -11 1 8 1 8 1 8 1 7 6 k 1 -6 1 9 2 48 1 8 1 -5 6 total A d 37 g 20 k 54 print (df20) T2012 2012 T2013 2013 T2014 2014 T2015 2015 T2016 2016 Ttotal \ A d 2 9 1 4 1 5 1 3 1 4 6 g 2 -15 1 6 1 6 1 6 1 5 6 k 1 -10 1 5 2 40 1 4 1 -9 6 total A d 25 g 8 k 30 的输出分配给相同的变量:

dfa = func(df10)
dfb = func(df20)

您也可以分配新变量:

{{1}}