我在字典中有3个数据框,其中键是月份标识符,值是数据框:
下面是数据帧和键的快照:
现在,对于每个唯一变量,我想捕获其在所有月份/数据框中的相关强度。 如果变量在df中具有相关值,则应将其捕获,否则该值将为0。类似于excel中的VLOOKUP。
最终数据框如下所示:
这对我来说用python实现似乎很复杂,所以有人可以帮我吗?
以下是用于生成示例数据并创建数据帧的字典的代码:
import pandas as pd
import numpy as np
df1 = pd.DataFrame([{'Variable_Name':'Pending_Disconnect','correlation': 0.553395448},
{'Variable_Name':'status_Active','correlation': 0.539464806},
{'Variable_Name':'days_active','correlation':0.414774231},
{'Variable_Name':'days_pend_disco','correlation':0.392915837},
{'Variable_Name':'prop_tenure','correlation':0.074321692},
{'Variable_Name':'abs_change_3m','correlation':0.062267386}
])
df2 = pd.DataFrame([{'Variable_Name':'Pending_Change','correlation': 0.043461995},
{'Variable_Name':'status_Active','correlation': 0.038057697},
{'Variable_Name':'ethnic','correlation':0.037503202},
{'Variable_Name':'days_active','correlation':0.037227245},
{'Variable_Name':'archetype_grp','correlation':0.035761434},
{'Variable_Name':'age_nan','correlation':0.035761434}
])
df3 = pd.DataFrame([{'Variable_Name':'active_frq_N','correlation':0.025697016},
{'Variable_Name':'active_frq_Y','correlation': 0.025697016},
{'Variable_Name':'ethnic','correlation':0.025195149},
{'Variable_Name':'ecgroup','correlation':0.023192408},
{'Variable_Name':'age','correlation':0.023121305},
{'Variable_Name':'archetype_nan','correlation':0.023121305}
])
dfs = [df1,df2,df3]
months = ['Jan - Feb 2018','Jan - Mar 2018','Jan - Apr 2018']
sample_dict = dict(zip(months,dfs))
答案 0 :(得分:1)
将pd.concat
用作:
df1.set_index('Variable_Name',inplace=True)
df2.set_index('Variable_Name',inplace=True)
df3.set_index('Variable_Name',inplace=True)
df = pd.concat([df1,df2,df3], axis=1, sort=False).fillna(0)
df.reset_index(inplace=True)
df.columns = ['Variable_Name','Jan - Feb 2018','Jan - Mar 2018','Jan - Apr 2018']
print(df)
Variable_Name Jan - Feb 2018 Jan - Mar 2018 Jan - Apr 2018
0 Pending_Disconnect 0.553395 0.000000 0.000000
1 status_Active 0.539465 0.038058 0.000000
2 days_active 0.414774 0.037227 0.000000
3 days_pend_disco 0.392916 0.000000 0.000000
4 prop_tenure 0.074322 0.000000 0.000000
5 abs_change_3m 0.062267 0.000000 0.000000
6 Pending_Change 0.000000 0.043462 0.000000
7 ethnic 0.000000 0.037503 0.025195
8 archetype_grp 0.000000 0.035761 0.000000
9 age_nan 0.000000 0.035761 0.000000
10 active_frq_N 0.000000 0.000000 0.025697
11 active_frq_Y 0.000000 0.000000 0.025697
12 ecgroup 0.000000 0.000000 0.023192
13 age 0.000000 0.000000 0.023121
14 archetype_nan 0.000000 0.000000 0.023121
答案 1 :(得分:1)
您可以替换数据框的列名,然后使用pd.concat
来连接数据框。
for key, df in sample_dict.items():
df.rename(columns={'correlation':'correlation '+ key}, inplace=True)
pd.concat(dfs)
编辑:您还可以省略字典,并从数据框列表中执行此操作。
for i, df in enumerate(dfs):
df.rename(columns={'correlation':'correlation '+ months[i]}, inplace=True)
pd.concat(dfs)
答案 2 :(得分:0)
我的最终代码是@onno和@Sandeep Kadapa的代码的组合:
final_df = pd.DataFrame()
for key, df in sample_dict.items():
df = sample_dict[key]
df = df.iloc[:,0:2]
df.rename(columns={'correlation':'correlation '+ key}, inplace=True)
final_df = pd.concat([final_df,df],axis = 1,sort = False).fillna(0)
非常感谢您的快速恢复。