熊猫加入值成为nan
添加坐标,但是数据框架没有一个。
我想在这个城市中 ]
。
' 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] })
我想将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的情况下添加列? 我是熊猫的新手,想成为数据分析师,请有人可以帮助我更正我的代码吗?谢谢。
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]
})
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
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该代码解决了问题。
然后重置第一个数据框的索引,然后,不用合并尝试使用concat并设置
axis = 1
,以便将其水平串联感谢在评论部分帮助的人。
This code resolved the problem.
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 horizontallyThanks to the guys who helped in the comment section.
您可以尝试
concat
或合并
You can try
concat
ormerge
看来您的一个框架将“城市”作为一列,而另一个则是索引。您需要在前者上运行
.set_index('city')
,或(给出了最新版本的pandas)添加on ='city'
tojoin()
参数。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) addon='City'
tojoin()
arguments.