3
替换python熊猫df值为第二个数据帧我是Python新手,因为我通常在R中编写脚本,因此正在学习适应Pandas数据框和细微差别。基于条件
我有两个口令列表,我把它们变成了数据框,因为我认为使用这种格式会更容易。
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}]
我然后把这些作为dataframes:
df1new = DataFrame(df1, columns=['test', '25th_percentile', 'mean', '50th_percentile','75th_percentile'])
df2new = DataFrame(df2)
现在,我想在df1new替换列“意味着”的内容,如果“测试” ==“ACT复合材料”和“意思是没有
我试过使用combine_first方法,但是我相信这需要更类似索引的数据框。 我也曾尝试:
if df1new['test'] == "ACT Composite" and df1new['mean'] == None:
df1new['mean'] == df2new['mean']
以及一个.replace()的变化。
任何意见将不胜感激! 提前谢谢!