简体   繁体   中英

How do I compare the two data frames and get the values?

x 数据框 y 数据框

The x data frame is information about departure and arrival, and the y data frame is latitude and longitude data for each location.

I try to calculate the distance between the origin and destination using the latitude and longitude data of start and end (eg, start_x, start_y, end_x, end_y).

How can I connect x and y to bring the latitude data that fits each code into the x data frame?

The notation is somewhat confusing, but I took it after the question's notation.

One way to do would be by merging your dataframes into a new one like so:

Dummy dataframes:

import pandas as pd

x=[300,500,300,600,700]
y=[400,400,700,700,400]

code=[300,400,500,600,700]

start=[100,101,102,103,104]

end=[110,111,112,113,114]

x={"x":x, "y":y}

y={"code":code, "start":start, "end":end}

x=pd.DataFrame(x)

y=pd.DataFrame(y)

This gives:

x
x y
0 300 400
1 500 400
2 300 700
3 600 700
4 700 400
y
code start end
0 300 100 110
1 400 101 111
2 500 102 112
3 600 103 113
4 700 104 114

Solution:

df = pd.merge(x,y,left_on="x",right_on="code").drop("code",axis=1)

df
x y start end
0 300 400 100 110
1 300 700 100 110
2 500 400 102 112
3 600 700 103 113
4 700 400 104 114
df = df.merge(y,left_on="y",right_on="code").drop("code",axis=1)

df
x y start_x end_x start_y end_y
0 300 400 100 110 101 111
1 500 400 102 112 101 111
2 700 400 104 114 101 111
3 300 700 100 110 104 114
4 600 700 103 113 104 114

Quick explanation:

The line df = pd.merge(...) creates the new dataframe by merging the left one (x) on the "x" column and the right one on the "code" column. The second line df = df.merge(...) takes the existing df as the left one, and uses its column "y" to merge the "code" column from the y dataframe.

The.drop("code",axis=1) is used to drop the unwanted "code" column resulting from the merging.

The _x and _y suffixes are added automatically when merging dataframes that have the same column names. To control it, use the "suffixe=.." option when calling the second merging (when the column with the same name are merging). In this case it works right with the default setting so no bothering with this if you use the x as right and y as left dataframes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM