0

I am trying to combine two dataframes.

new = pd.DataFrame()
new['a'] = df['totalconfirmed'][386:]
new['b'] = df['totalrecovered'][386:]

compare['actual_infected'] = new['a']
compare['actual_recovered'] = new['b']

OUTPUT is for compare.head() :

actual_infected actual_recovered
  30081975      29056435
  30133634      29120804
  30182402      29185623
  30232246      29243489
  30278744      29302029

prediction is a dataframe that I calculated using a SIR model. It has three columns and has the same number of rows as compare. I have also checked the datatypes for EVERY column in both the dataframes and all of them are Series even after np.array.

This output is after I have transformed my new['a'] and new['b'] to array using np.array. But when I try to combine this dataframe with another dataframe containing the predicted infected and recovered values I get:

df1 = [prediction, compare]
result = pd.concat(df1, ignore_index = True)
print(result)
date         pred_infected  pred_recovered   actual_infected     actual_recovered
2021-02-19   1.295474e+08      34268136.0           NaN               NaN
2021-02-20   4.345577e+08      53006146.0           NaN               NaN
2021-02-21   8.682628e+08      100374686.0          NaN               NaN
2021-02-22   1.074353e+09      171983070.0          NaN               NaN
2021-02-23   1.083615e+09      250319518.0          NaN               NaN
 ...           ...               ...               ...                ...   
 NaN            NaN               NaN            30081975            29056435  
 NaN            NaN               NaN            30133634            29120804 
 NaN            NaN               NaN            30182402            29185623 
 NaN            NaN               NaN            30232246            29243489  
 NaN            NaN               NaN            30278744            29302029  

I don't understand what is there left to do.

  • IIUC then try `pd.concat(df1, ignore_index = True,axis=1)` – Anurag Dabas Jun 28 '21 at 05:10
  • You should be able to find an answer from a similar situation here: https://stackoverflow.com/questions/40339886/pandas-concat-generates-nan-values – Hultan Jun 28 '21 at 05:11
  • Use `df1 = [prediction.reset_index(drop=True), compare.reset_index(drop=True)]` and then `pd.concat(df1, ignore_index = True,axis=1)` – jezrael Jun 28 '21 at 05:12

0 Answers0