gpt4 book ai didi

python - 没有迭代的两个数据帧的交集

转载 作者:太空宇宙 更新时间:2023-11-04 04:14:45 25 4
gpt4 key购买 nike

所以我有两个df

第一:

    Latitude    Longitude   Area
0 -25.66026 28.0914 HappyPlace
1 -25.67923 28.10525 SadPlace
2 -30.68456 19.21694 AveragePlace
3 -30.12345 22.34256 CoolPlace
4 -15.12546 17.12365 BadPlace

第二点:

    Latitude    Longitude   Population
0 -25.66026 28.0914 5000
1 -25.14568 28.10525 1750
2 -30.68456 19.21694 6000
3 -30.65375 22.34256 8000
4 -15.90458 17.12365 5600

我想得到具有相同纬度/经度的地方,所以我知道人口。 最重要的是,对于我的实际项目,我只需要交叉点

结果 df:

    Latitude    Longitude   Area
0 -25.66026 28.0914 HappyPlace
2 -30.68456 19.21694 AveragePlace

我试过:

pd.merge(df1, df2, on=['LATITUDE'], how='inner')

不工作返回奇怪的df

set(df1['LATITUDE']).intersection(set(df2['LATITUDE'))
df1[(df1['LATITUDE'] == df2['LATITUDE'])]
df1.where(df1.LATITUDE == df2.LATITUDE)

全部返回 ValueError: Can only compare identically-labeled Series objects

(实际 Df 非常非常大,两列都是 float )

最佳答案

pd.merge()KeyError 而失败,因为 LATITUDE 是错误的键。

以下 MCVE 按预期工作。

import pandas as pd
import numpy as np
print(pd.__version__)

df1_string = """-25.66026 28.0914 HappyPlace
-25.67923 28.10525 SadPlace
-30.68456 19.21694 AveragePlace
-30.12345 22.34256 CoolPlace
-15.12546 17.12365 BadPlace"""

df2_string = """-25.66026 28.0914 5000
-25.14568 28.10525 1750
-30.68456 19.21694 6000
-30.65375 22.34256 8000
-15.90458 17.12365 5600"""

df1 = pd.DataFrame([x.split() for x in df1_string.split('\n')], columns=['Latitude', 'Longitude', 'Population'])
df2 = pd.DataFrame([x.split() for x in df2_string.split('\n')], columns=['Latitude', 'Longitude', 'Population'])
result = pd.merge(df1, df2, on=['Latitude'], how='inner')
print(set(df1['Latitude']).intersection(set(df2['Latitude'])))
print(df1[(df1['Latitude'] == df2['Latitude'])])
print(df1.where(df1.Latitude == df2.Latitude))

print(result)

产生

0.24.2
{'-25.66026', '-30.68456'}
Latitude Longitude Population
0 -25.66026 28.0914 HappyPlace
2 -30.68456 19.21694 AveragePlace
Latitude Longitude Population
0 -25.66026 28.0914 HappyPlace
1 NaN NaN NaN
2 -30.68456 19.21694 AveragePlace
3 NaN NaN NaN
4 NaN NaN NaN
Latitude Longitude_x Population_x Longitude_y Population_y
0 -25.66026 28.0914 HappyPlace 28.0914 5000
1 -30.68456 19.21694 AveragePlace 19.21694 6000

关于python - 没有迭代的两个数据帧的交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55654911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com