[英]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
.我有一个带有许多数据列的大型 dataframe
df1
,其中两个是dates
和colNum
。 I have built a second dataframe df2
which spans the date range and colNum
of df1
.我已经构建了第二个 dataframe
df2
,它跨越了df1
的日期范围和colNum
。 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
.我现在想用 df1 的第三列(许多其他数据列中的任何一个)填充
df2
,这些列满足df1
中的dates
和colNum
的标准,这些colNum
与df2
的dateIndex
和df1
匹配。
I've tried various incarnations of MERGE
with no success.我尝试了
MERGE
的各种化身,但没有成功。
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.我可以遍历组合,但
df1
非常大(270k,2k),因此从df1
的列之一填充一个df2
需要永远,更不用说所有列了。
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]
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 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')
#根据您的方便保留“左”或“内”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.