在彼此旁边汇总两列

时间:2018-06-12 10:05:07

标签: python-3.x pandas concat

我已经编写了一个通过input()收集一些数据的函数,它对手头的问题并不重要。但最后我需要.concat两列。 到目前为止,我已经在一定程度上使它工作,但它并不完美。 {def visualise_country():

RewriteRule ^(.*)$ index.php [QSA,L]

Target_frame只是一个元组,包含收集列所需的收集信息。

输出:

data = pd.read_csv('tas_pr_1991_2015_AC.csv')

target_frame = get_info()
df1 = pd.DataFrame(data.loc[data['country'] == target_frame[0]])
df1 = pd.DataFrame(df1.loc[df1['year'] == int(target_frame[2])])
df1 = df1[target_frame[4]]

df2 = pd.DataFrame(data.loc[data['country'] == target_frame[1]])
df2 = pd.DataFrame(df2.loc[df2['year'] == int(target_frame[3])])
df2 = df2[target_frame[4]]

frame_list = [df1,df2]

df = pd.concat(frame_list, axis=1)

print("Data for {} in comparison with {}. Comparison years for {}: {} and {}: ".format(target_frame[0],target_frame[1],target_frame[0],target_frame[2],target_frame[1],target_frame[3]))

return df}

期望的输出:

1 - NaN 
2 - NaN 
3 - NaN
4 - NaN
NaN - 5
NaN - 6
NaN - 7
NaN - 8

1 个答案:

答案 0 :(得分:2)

在所有DataFrames中需要相同的索引值:

frame_list = [x.reset_index(drop=True) for x in [df1,df2]]

或者:

df1.index = df2.index
frame_list = [df1,df2]
df = pd.concat(frame_list, axis=1)

或者:

df1 = df1[target_frame[4]].reset_index(drop=True)
df2 = df2[target_frame[4]].reset_index(drop=True)
frame_list = [df1,df2]
df = pd.concat(frame_list, axis=1)