简体   繁体   English

如何通过匹配 df1 中与 df2 索引和列名匹配的列值来用 df1 中的数据填充 df2

[英]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 ,其中两个是datescolNum 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中的datescolNum的标准,这些colNumdf2dateIndexdf1匹配。

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]

Input输入

Desired Output所需 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 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.

相关问题 pandas 如何从 df2 获取 df1 的值,而 df1 和 df2 的值在列上重叠 - pandas how to get values from df2 for df1 while df1 and df2 have values overlapped on column(s) 从 DF2 替换 DF1 中的值 - Replace values in DF1 from DF2 如果 df2 中不存在列,如何将列从 df1 添加到 df2,否则什么也不做 - How to add a column from df1 to df2 if it not present in df2, else do nothing 从 DF1 中选择行,其中列值与 DF2 中的列中的值匹配 - Selecting rows from DF1 where column values match values from a column from DF2 如何用来自另一个 dataframe (df2) 的信息填充 dataframe (df1) 的列? 就在 df1 和 df2 中的两列信息匹配时? - How to fill a column of a dataframe (df1) with info from another dataframe (df2)? Just when two column info matches in df1 and df2? 如何将两个不同的数据框 df1 df2 与特定列(列 w)进行比较,并从 df2 更新 df1 中匹配的行列 AD - how to compare two different data frames df1 df2 with specific column ( column w) and update the matched rows column AD in df1 from df2 如何向 dataframe (df1) 添加一个新列,这是另一个 dataframe (df2) 中 df1 的多个查找值的总和 - How can I add a new column to a dataframe (df1) that is the sum of multiple lookup values from df1 in another dataframe (df2) 如果两个不同数据帧中两列的值匹配,则将df2中另一列的值复制到df1中的列 - If values from two columns in two different data frames match then copy values from another column in df2 to column in df1 在DF2列值与DF1索引匹配的pandas DataFrame1中设置新的列值 - Set new column values in pandas DataFrame1 where DF2 column values match DF1 index 根据 Date 列将 df1 中的列添加到 df2 中,如果缺少 df1 [Date] 条目,则填写 na - Add column from df1 into df2 based on the Date column and fill na if df1 [Date] entries are missing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM