如何根据特定值中数据帧的相似性来更新数据帧列?

2024-05-18 06:33:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个用户dataframe,其中包含凌乱的城市(有些也丢失了)名称,如下所示:

a = [['New York', '10460', '21', '321321.12' ], 
['Brooklyn', '11219', '03', '2432567.52'], [' ', '11363', '320', '34325.543]]
userdf = pd.DataFrame(a, columns=['City', 'Post Code', 'Lat', 'Long])
userdf

现在,我想通过查看location数据框中的相应邮政编码,根据邮政编码修复City、Lat和Long列中的名称,官方详细信息如下:

location = [['Bronx', '10460', '21', '321321.12' ], 
['Brooklyn', '11219', '03', '2432567.52'], [' ', '11363', '320', '34325.543]]
locationdf = pd.DataFrame(location, columns=['City', 'Post Code', 'Lat', 'Long])

我试过多种方法,但都失败了。如何根据条件userdf['Post Code']等于locationdf['Post Code']使用locationdf中的值更新userdf中的某些列。你知道吗


Tags: columns用户名称citydataframecodelocationpost