I have two pandas.DataFrame
of the form
Two data frames are:
df1 = pd.DataFrame(4*[100,100,100],4*[0,1,2])
df2 = pd.DataFrame([1,2,3,4,5,6,7,8,9],[0,1,2,0,1,0,2,1,2])
I want to classify df1 and df2 with the same index based on the index of df1. And I want to put 0 in the blank. Here is my expected result:
index df1 df2
0 100 1
1 100 2
2 100 3
0 100 4
1 100 5
2 100 0
0 100 6
1 100 0
2 100 7
0 100 0
1 100 8
2 100 9
Is this possible?
CodePudding user response:
I think what you are looking for is pandas.DataFrame.sort_values
check sort_values for reference.
CodePudding user response:
If you don't mind with index, such as this kind of format, :
Int64Index([0, 0, 0, 1, 1, 1, 2, 2, 2], dtype='int64')
You can try this :
new_df = pd.merge(df1, df2, left_index=True, right_index=True, how='right').drop_duplicates()
new_df.rename(columns={'0_x' : 'df1',
'0_y' : 'df2'})
It will give you :
df1 df2
0 100 1
0 100 4
0 100 6
1 100 2
1 100 5
1 100 8
2 100 3
2 100 7
2 100 9