简体   繁体   English

将python pandas df替换为基于条件的第二个数据帧的值

[英]Replace python pandas df with values of a second dataframe based with condition

I am new to python as I normally write scripts in R and therefore am learning to adjust to Pandas dataframes and nuances. 我是python的新手,因为我通常在R中编写脚本,因此我正在学习如何适应Pandas数据帧和细微差别。

I have two lists of dicts that I turned into dataframes as I thought it would be easier to work with in that format. 我有两个dicts列表,我转变为数据帧,因为我认为以这种格式更容易使用。

df1= [{u'test': u'SAT Math', u'25th_percentile': None, u'75th_percentile': None, u'50th_percentile': None, u'mean': 404}, {u'test': u'SAT Verbal', u'25th_percentile': None, u'75th_percentile': None, u'50th_percentile': None, u'mean': 355}, {u'test': u'SAT Writing', u'25th_percentile': None, u'75th_percentile': None, u'50th_percentile': None, u'mean': 363}, {u'test': u'SAT Composite', u'25th_percentile': None, u'75th_percentile': None, u'50th_percentile': None, u'mean': 1122}, {u'test': u'ACT Math', u'25th_percentile': None, u'75th_percentile': None, u'50th_percentile': None, u'mean': None}, {u'test': u'ACT English', u'25th_percentile': None, u'75th_percentile': None, u'50th_percentile': None, u'mean': None}, {u'test': u'ACT Reading', u'25th_percentile': None, u'75th_percentile': None, u'50th_percentile': None, u'mean': None}, {u'test': u'ACT Science', u'25th_percentile': None, u'75th_percentile': None, u'50th_percentile': None, u'mean': None}, {u'test': u'ACT Composite', u'25th_percentile': None, u'75th_percentile': None, u'50th_percentile': None, u'mean': None}]


df2 = [{u'test': u'SAT Composite', u'mean': 1981}, {u'test': u'ACT Composite', u'mean': 29.6}]

I then put these as dataframes: 然后我将这些作为数据帧:

df1new = DataFrame(df1, columns=['test', '25th_percentile', 'mean', '50th_percentile','75th_percentile'])
df2new = DataFrame(df2)

Now, I would like to replace the contents of the column 'mean' in df1new if 'test' == "ACT Composite" and 'mean' is None 现在,如果'test'==“ACT Composite”并且'mean'为None,我想替换df1new中'mean'列的内容

I have tried to use a combine_first approach, however I believe this requires the dataframes to be more similarly indexed. 我曾尝试使用combine_first方法,但我相信这需要对数据帧进行更类似的索引。 I have also tried: 我也尝试过:

if df1new['test'] == "ACT Composite" and df1new['mean'] == None:
            df1new['mean'] == df2new['mean']

as well as a .replace() variation. 以及.replace()变体。

Any advice would be greatly appreciated! 任何建议将不胜感激! Thank you in advance! 先感谢您!

maybe this: 也许这个:

idx = (df1new.test == 'ACT Composite') & df1new['mean'].isnull()
df1new['mean'][idx] = df2new['mean'][1]

I added a [1] up there because i suppose that is what you want, the mean value corresponding to ACT Composite in df2new . 我加了[1]在那里,因为我想这是你想要什么,在mean对应值ACT Compositedf2new it could also be written as 它也可以写成

df1new['mean'][idx] = df2new['mean'][df2new.test == 'ACT Composite']

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

相关问题 根据第二个df替换python pandas列中的值 - Replace values in python pandas column based on second df 如何根据第二个 Dataframe 值的条件替换 Dataframe 列值 - How to Replace Dataframe Column Values Based on Condition of Second Dataframe Values Pandas:根据条件用另一个数据帧值替换数据帧中的值 - Pandas: replace values in dataframe with another dataframes values based on condition 根据条件,用相应的列名替换 pandas 数据框中的特定值, - Replace specific values in pandas dataframe with the corresponding column name, based on a condition, 将 pandas dataframe 中的列切片中的值替换为基于条件的值 - Replace values in a slice of columns in a pandas dataframe with a value based on a condition 根据条件用不同的替换字典替换熊猫数据框列中的值 - Replace values in pandas dataframe column with different replacement dict based on condition Pandas DataFrame:根据条件替换列中的所有值 - Pandas DataFrame: replace all values in a column, based on condition 替换由第一个 df - pandas 确定的第二个 df 中的值 - Replace values in second df determined by first df - pandas 根据条件-(Python,Pandas)替换合并数据框中的NaN - replace NaNs in the merged dataframe based on the condition --(Python,Pandas) 基于另一个 DF 替换 pandas 列值 - Replace pandas column values based on another DF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM