简体   繁体   中英

How to fill df2 with data from df1 by matching column values from df1 which match df2 index and column names

I have a large dataframe df1 with many data columns, two of which are dates and colNum . I have built a second dataframe df2 which spans the date range and colNum of df1 . I now want to fill df2 with a third column (any of the many other data columns) of df1 which meet the criteria of dates and colNum from df1 that match dateIndex and colNum of df2 .

I've tried various incarnations of MERGE with no success.

I can loop through the combinations, but df1 is very large (270k, 2k) so it takes forever to do fill one df2 from one of df1 's columns, let alone all of them.

Slow looping version

dataList = ['revt']
for i in dataList:
    goodRows = df1.index[~np.isnan(df1[i])].tolist()
    for j in goodRows:
        df2.loc[df1['dates'][j], str(df1['colNum'][j])] = df1[i][j]

Input

Desired Output

convert index to column eg

df1.reset_index() #as per your statement date seems to be in index df2.rest_index() df2 = pd.merge(df2, df1, on = ['dateIndex', 'colNum'], how = 'left') #keep either "left" or "inner" as per your convenience

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM