我想知道是否有最快的代碼替換兩個for循環,假設df大小非常大。在我的實際情況中,每個數據幀都是200行和25列。基於列的子集合並和更新數據幀
data_df1 = np.array([['Name','Unit','Attribute','Date'],['a','A',1,2014],['b','B',2,2015],['c','C',3,2016],\
['d','D',4,2017],['e','E',5,2018]])
data_df2 = np.array([['Name','Unit','Date'],['a','F',2019],['b','G',2020],['e','H',2021],\
['f','I',2022]])
df1 = pd.DataFrame(data=data_df1)
print('df1:')
print(df1)
df2 = pd.DataFrame(data=data_df2)
print('df2:')
print(df2)
row_df1 = [1,2,5]
col_df1 = [1,3]
row_df2 = [1,2,3]
col_df2 = [1,2]
for i in range(0,len(row_df1)):
for j in range(0, len(col_df1)):
df1.set_value(row_df1[i],col_df1[j], df2.loc[row_df2[i],col_df2[j]])
print('df1 after operation:')
print(df1)
預期輸出:
df1:
0 1 2 3
0 Name Unit Attribute Date
1 a A 1 2014
2 b B 2 2015
3 c C 3 2016
4 d D 4 2017
5 e E 5 2018
df2:
0 1 2
0 Name Unit Date
1 a F 2019
2 b G 2020
3 e H 2021
4 f I 2022
df1 after operation:
0 1 2 3
0 Name Unit Attribute Date
1 a F 1 2019
2 b G 2 2020
3 c C 3 2016
4 d D 4 2017
5 e H 5 2021
我曾嘗試:
df1.loc[[1,2,5],[1,3]] = df2.loc[[1,2,3],[1,2]]
print('df1:')
print(df1)
print('df2:')
print(df2)
但結果如下。有意想不到的南。
df1:
0 1 2 3
0 Name Unit Attribute Date
1 a F 1 NaN
2 b G 2 NaN
3 c C 3 2016
4 d D 4 2017
5 e NaN 5 NaN
df2:
0 1 2
0 Name Unit Date
1 a F 2019
2 b G 2020
3 e H 2021
4 f I 2022
在此先感謝任何幫助。
@John我已經告訴你如何得到你的輸出。 –
@John如果你要堅持並說你得到了錯誤的答案,那是因爲你的數據,而不是我的問題。我應該注意到這是你第二次這樣做,並拒絕承認回答你的問題和隨之而來的破壞性數據所付出的努力。 –
@COLDSPEED我非常感謝你的幫助。在使用df1.T.reset_index()。T之後,我在筆記本中看到的結果只是一個事實,沒有第一行索引0,1,2,3,即'Name','Unit', .etc是作爲df1.columns.values返回的結果。 – John