Skip to content Skip to sidebar Skip to footer

Problem With Using Merge_asof From Pandas

I have these two dataframes: DF1= Inflow 0 9810998109 1 5591255912 2 7394273942 3 7866678666 4 1820118202 5 9812198109 6 9810998101 7 4304043040 8 9810998121 DF2=

Solution 1:

This is your solution:

In your first dataFrame(df1) you have only column while Second(df2) has two, while doing pd.merge you have to choose outer, which is a union of the keys. This means all of the indexes are shown and where it has missing cols it keeps them as NaN.

>>>df1
       Inflow
0  9810998109
1  5591255912
2  7394273942
3  7866678666
4  1820118202
5  9812198109
6  9810998101
7  4304043040
8  9810998121
>>>df2
       Inflow  mi_to_zcta5
0  3371433756    11.469054
1  1790118201    24.882142
>>>>>>>>>>>>pd.merge( df1, df2, on=['Inflow'], how='outer')
        Inflow  mi_to_zcta5
0   9810998109          NaN
1   5591255912          NaN
2   7394273942          NaN
3   7866678666          NaN
4   1820118202          NaN
5   9812198109          NaN
6   9810998101          NaN
7   4304043040          NaN
8   9810998121          NaN
9   3371433756    11.469054
10  1790118201    24.882142

Note: you can not merge on the Key 'mi_to_zcta5 as this is not present on df

Post a Comment for "Problem With Using Merge_asof From Pandas"