简体   繁体   English

用基于其中一列的另一个数据框替换数据框中的值

[英]Replacing values in data frame with another data frame based on one of the columns

I have two dataframes, one that contains some information and a shorter dataframe that contains some amendments I want to make to the first.我有两个数据框,一个包含一些信息,一个更短的数据框包含我想对第一个进行的一些修改。 I have tried something similar to that written below.我尝试过类似于下面写的东西。

for x in df['var']:
    if x in df2['var']:
         df['var2'] == df2['var2']

For example:例如:

---> df
        num_legs  num_wings  num_specimen_seen
falcon         2          2                 10
dog            4          0                  2
spider         8          0                  1
fish           0          0                  8

---> df1
        num_legs  num_wings  num_specimen_seen
dog            4          0                  5
spider         8          0                  9

---> output
        num_legs  num_wings  num_specimen_seen
falcon         2          2                 10
dog            4          0                  5
spider         8          0                  9
fish           0          0                  8

Have you tried df.update()你试过df.update()

df.update(df1)



        num_legs  num_wings  num_specimen_seen
falcon       2.0        2.0               10.0
dog          4.0        0.0                5.0
spider       8.0        0.0                9.0
fish         0.0        0.0                8.0

Alternative选择

import numpy as np
df[df.index.isin(df1.index)]=np.nan
df1.combine_first(df)

How it works这个怎么运作

#Find shared index between the two dataframes which are in df . #Find shared index between the two dataframes which are in df

df[df.index.isin(df1.index)]

#Make them NaN


df[df.index.isin(df1.index)]=np.nan

#Use [.combine_first][1] to fill them

df1.combine_first(df)

暂无
暂无

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

相关问题 用另一个数据框中的值更新一个数据框中的列 - Update columns in one data frame with values from another data frame 根据 pandas 中另一个数据帧中的某些条件将值从一个数据帧拆分到另一个数据帧 - Splitting values from one data frame to another data frame based on certain conditions in another data frame in pandas 用其列的顺序替换Pandas数据框中的值 - Replacing values in a Pandas data frame with the order of their columns 根据一列按数据分组,然后使用数据框中的值构建行 - Group by the data based on the one columns and then build the rows with the values in data frame 检查一个数据帧的任何值(多列)是否在另一数据帧的任何值(多列)中 - Check if any value ( multiple columns) of one data frame exists in any values (multiple columns) of another data frame 如何根据合并的数据框之一的两列的值在熊猫数据框中添加值 - How to add values in a pandas data frame based on values of two columns of one of the data frame merged 大熊猫:根据索引和列将一个数据框的值替换为另一数据框的值 - Pandas: replace values of one data frame with values of another data frame based on index and column 如何根据列名将一个数据框中的列值复制到另一个数据框中? - How do I copy the value of columns in one data frame to another data frame based on column names? 根据条件将值从一个pandas数据帧替换为另一个pandas数据帧 - Substitute values from one pandas data frame to another based on condition Python Pandas 数据帧 基于另一列的计数值 - Python Pandas Data Frame Count values of one column based on another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM