To merge two DataFrames based on their index in Python using pandas, you can use the merge function with the left_index
and right_index
parameters set to True.
Here is an example of how to do it:
main.py305 chars11 lines
Output:
main.py42 chars4 lines
In the example above, we create two DataFrames, df1
and df2
, with different index values. Then we use the merge
function and specify left_index=True
and right_index=True
to perform the merge based on the index. The resulting merged DataFrame is stored in merged_df
.
Note that the merge will include only the matching rows from both DataFrames based on their index values.
gistlibby LogSnag