Adding two data frames but retaining index order of first dataframe
I have made two sample data frames, both have same columns names, (Buckets and Amounts) and for both data frames "Buckets" is the index.
df1 has more rows(complete set) and df2 has subset of rows.
when i add df2 into df1, the operation works perfectly but index locations move and are not in the same order of original df1's Buckets=['3M','6M','9M','1Y','2Y','3Y'].. they are like 1Y,2Y,3M etc ..how can i retain ds1 original index order after the add operation too?
Buckets=['3M','6M','9M','1Y','2Y','3Y']
Amount1=[1,2,3,4,5,6]
Buckets2=['1Y','2Y','3Y']
Amount2=[4,5,6]
df1 = pd.DataFrame(columns = ['Buckets','Amount'])
df1.loc[:,'Amount']=Amount1
df1.loc[:,'Buckets']=Buckets
df1.set_index('Buckets',inplace=True)
df2 = pd.DataFrame(columns = ['Buckets','Amount'])
df2.loc[:,'Amount']=Amount2
df2.loc[:,'Buckets']=Buckets2
df2.set_index('Buckets',inplace=True)
df1=df1.add(df2,fill_value=0)
print(df1)
from Recent Questions - Stack Overflow https://ift.tt/3DmrSGs
https://ift.tt/eA8V8J
Comments
Post a Comment