熊猫加入值成为nan

发布于 2025-02-13 22:14:32 字数 1202 浏览 3 评论 0原文

添加坐标,但是数据框架没有一个。

我想在这个城市中 ]

' pd.dataframe({ “城市”:['dasmarinas',“将军”,“ imus”,“ las pinas”,“ makati”,“ mandaluyong”,“ Manila”,“ Manila”,“ Muntinlupa”,“ Paranaque”,“ Paranaque”,“ Pasay”,“ Pasay”,“ Pasig”,“ Pasig” ,“ Quezon City”,“ Silang”,“ Taguig”], “纬度”:[14.2990,14.3214,14.4064,14.4445,14.5547,14.5794,14.5995,14.4081,14.4793,14.4793,14.5378,14.5764 'Longitude': [120.9590, 120.9073, 120.9405, 120.9939, 121.0244, 121.0359, 120.9842, 121.0415, 121.0198, 121.0014, 121.0851, 121.0437, 120.9687, 121.0509] })

group_metro_manila

我想将City_location latitude和经度添加到其指定的城市中 city_location

所以我要做的是加入city_location to group_metro_metro_manil_manila

merge_df = = group_metro_manila.join(city_location [['city','latitude','atitude']]。set_index('city'),on ='city'),

但值变为nan

加入,但值变为nan

有人可以启发我如何在不制作NAN的情况下添加列? 我是熊猫的新手,想成为数据分析师,请有人可以帮助我更正我的代码吗?谢谢。

I want to add coordinates to this cities, but the dataframe does not have one..

group_metro_manila = metro_manila.groupby(['City'])[['Price (PHP)', 'Floor_area (sqm)', 'Price Per SQM']].mean().sort_values('Price (PHP)')

So I manually created them...

city_location = pd.DataFrame({
'City': ['Dasmarinas', 'General Trias', 'Imus', 'Las Pinas', 'Makati', 'Mandaluyong','Manila', 'Muntinlupa','Paranaque','Pasay', 'Pasig','Quezon City', 'Silang', 'Taguig'],
'Latitude': [14.2990, 14.3214, 14.4064, 14.4445, 14.5547, 14.5794, 14.5995, 14.4081, 14.4793, 14.5378, 14.5764, 14.6760, 14.2142, 14.5176],
'Longitude': [120.9590, 120.9073, 120.9405, 120.9939, 121.0244, 121.0359, 120.9842, 121.0415, 121.0198, 121.0014, 121.0851, 121.0437, 120.9687, 121.0509]
})

group_metro_manila

I want to add the city_location latitude and longitude to their designated city
city_location

So what I do is I join city_location to group_metro_manila

merge_df = group_metro_manila.join(city_location[['City','Latitude','Longitude']].set_index('City'), on='City')

but the values becomes NaN

Join but values becomes NaN

can someone enlighten me on how to add columns without making the values NaN?
I am new to pandas and want to become data analyst, please can someone help me correct my code? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神爱温柔 2025-02-20 22:14:35

该代码解决了问题。

dataframe = [group_metro_manila_sorted.reset_index(drop=True), city_location_sorted[['Latitude', 'Longitude']]]

concat_df = pd.concat(dataframe, join='inner',axis=1)

然后重置第一个数据框的索引,然后,不用合并尝试使用concat并设置axis = 1,以便将其水平串联

“

感谢在评论部分帮助的人。

This code resolved the problem.

dataframe = [group_metro_manila_sorted.reset_index(drop=True), city_location_sorted[['Latitude', 'Longitude']]]

concat_df = pd.concat(dataframe, join='inner',axis=1)

Reset the index of the first dataframe, then, instead of merge try to use concat and set the axis=1 so that it will be concatenated horizontally

concat_df

Thanks to the guys who helped in the comment section.

纸短情长 2025-02-20 22:14:34

您可以尝试concat合并

pd.concat([group_metro_manila, city_location.set_index('City')], axis=1)
# or
group_metro_manila.merge(city_location, left_index=True, right_on='City', how='left')

You can try concat or merge

pd.concat([group_metro_manila, city_location.set_index('City')], axis=1)
# or
group_metro_manila.merge(city_location, left_index=True, right_on='City', how='left')
烟酒忠诚 2025-02-20 22:14:34

看来您的一个框架将“城市”作为一列,而另一个则是索引。您需要在前者上运行.set_index('city'),或(给出了最新版本的pandas)添加on ='city' to join()参数。

It looks like one of your frames has 'City' as a column and other as index. You'll need to either run .set_index('City') on the former or (given a recent enough version of pandas) add on='City' to join() arguments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文