简体   繁体   English

我有两个数据框 df 和 df1 具有相同的 id 列,但两个数据框中的列名不同,我想在第二个 dataframe 中更新值

[英]I have a two data frames df and df1 have same id columns but different column names in both data frames and I want update values in second dataframe

在此处输入图像描述在此处输入图像描述

I want to update df1 as per updated df1, if df1 have nans replace with values if df have values matched with ID column on both data frames.我想根据更新的 df1 更新 df1,如果 df1 有 nans 替换为值,如果 df 的值与两个数据帧上的 ID 列匹配。

My Expected Output is in the second image nan replaces with values I have provided sample below我的预期 Output 在第二张图片中 nan 替换为我在下面提供的示例的值

ID QD QP QE ID QD QP QE

101 4 6 4 101 4 6 4

102 5 8 5 102 5 8 5

103 7 6 6 103 7 6 6

104 8 3 5 104 8 3 5

105 4 2 5 105 4 2 5

If your ID columns is sorted and these two columns are one-to-one correspondence, you can use如果你的ID列是排序好的,这两列是一一对应的,你可以使用

df1[df1.isnull()] = df.values
print(df1)

    ID   QD   QP   QE
0  101  4.0  6.0  4.0
1  102  5.0  8.0  5.0
2  103  7.0  6.0  6.0
3  104  8.0  3.0  5.0
4  105  4.0  2.0  5.0

If not, you'd better set the ID column as index and choose one among fillna method, combine_first method and update method to update column according to index.如果没有,最好将ID列设置为索引,并在fillna方法、 combine_first方法和update方法中选择一种根据索引更新列。

df1 = df1.set_index('ID')

# fillna
df1 = df1.fillna(df.set_index('ID').set_axis(df1.columns, axis=1)).reset_index()

# combine_first, if df is bigger than your original df1,
# the additional rows and columns are added
df1 = df1.combine_first(df.set_index('ID').set_axis(df1.columns, axis=1)).reset_index()

# update method will modify data inplace,
# you need to do reset index in separate step
df1.update(df.set_index('ID').set_axis(df1.columns, axis=1))
df1.reset_index()
print(df1)


    ID   QD   QP   QE
0  101  4.0  6.0  4.0
1  102  5.0  8.0  5.0
2  103  7.0  6.0  6.0
3  104  8.0  3.0  5.0
4  105  4.0  2.0  5.0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果两个不同数据帧中两列的值匹配,则将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 我有两个数据框(DF1)和(DF2)。 我想替换 (DF2) 中与 (DF1) 的两列上的条件匹配的列的值 - I have two dataframes (DF1) and (DF2). I want to substitute values for a column in (DF2) that match criteria on two columns of (DF1) 我可以使用df1中的一列和df2中的单元格中的任何值之一来连接两个数据帧吗? - Can I join two data frames using one column in df1 and one of any values in a cell in 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 我有两个数据框 df1 和 df2,我需要使用 df2 中的键过滤掉 df1,使用 df2 中的开始和结束日期,我需要得到像 df3 这样的结果 - I have two data frames df1 and df2, I need to filter out df1 using keys in df2 using start and end dates in df2, I need to get a result like df3 如果找到匹配项,则比较两个不同数据框中的列,将电子邮件从df2复制到df1 - Compare columns in two different data frames if match found copy email from df2 to df1 Pandas - 匹配两个数据帧中的两列,并在df1中创建新列 - Pandas - match two columns from two data frames and create new column in df1 获取 df['num'] 和 df1['num'] 中存在的公共数据并合并两个数据帧中相同的数据 - Get common data present in df['num'] and df1['num'] and merge the data which is the same from both the data Frames Python>Pandas>对具有相同列名、相同索引值但索引长度不同的不同数据框中的列求和 - Python>Pandas>Summing columns in different data frames which have same column names, same index values but not same same length of index 如何基于两个不同数据帧的列创建新的df? - How to create new df based on columns of two different data frames?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM