2013-12-09 237 views
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()的變化。

任何意見將不勝感激! 提前謝謝!

回答

1

也許這:

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

我加了一個[1]那裏,因爲我想這是你想要的,在df2new對應ACT Compositemean值。它也可以寫成

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