熊猫将值从另一个数据框中复制到我的数据框中

时间:2019-07-09 21:10:07

标签: python pandas dataframe

我有2个数据框:var minDate = dateDim.bottom(1)[0].day; // Date object var maxDate = dateDim.top(1)[0].day; .x(d3.scaleTime().domain([minDate, maxDate])) 和url,df_mentions和url。 我需要不断使用媒体中包含的信息更新media

df_mentions

它们看起来像这样(但每天有近1000行) df_mentions

媒体

media

并且我需要检查链接的来源是否包含在媒体中,并从链接中提取数据以填充df_mentions并获得以下结果:

预期: enter image description here

我所做的是:

Mentions=['https://www.lemonde.fr/football/article/2019/07/08/coupe-du-monde-feminine-2109-au-sein-de-chaque-equipe-j-ai-vu-de-grandes-joueuses_5486741_1616938.html','https://www.telegraph.co.uk/world-cup/2019/06/12/womens-world-cup-2019-groups-complete-guide-teams-players-rankings/','https://www.washingtonpost.com/sports/dcunited/us-womens-world-cup-champs-arrive-home-ahead-of-parade/2019/07/08/48df1a84-a1e3-11e9-a767-d7ab84aef3e9_story.html?utm_term=.8f474bba8a1a']
Date=['08/07/2019','08/07/2019','08/07/2019']
Publication=['','','']
Country=['','','']
Foundation=['','','']
Is_in_media=['','','']
df_mentions=pd.DataFrame()
df_mentions['Mentions']=Mentions
df_mentions['Date']=Date
df_mentions['Source']=Source
df_mentions['Country']=Country
df_mentions['Foundation']=Foundation
df_mentions['Is_in_media']=Is_in_media

Source=['New York times','Lemonde','Washington Post']
Link=['https://www.nytimes.com/','https://www.lemonde.fr/','https://www.washingtonpost.com/']
Country=['USA','France','USA']
Foundation=['1851','1944','1877']
media=pd.DataFrame()
media['Source']=Source
media['Link']=Link
media['Country']=Country
media['Foundation']=Foundation
media

但是它一次可以在我的笔记本电脑上工作,如果我关闭笔记本电脑会出现错误,则我正在使用Pandas 0.24.0。 有没有更好的方法来做到这一点并让它一直工作?

提前谢谢! 所有帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

您可以做的一件事是提取df_mentions中的URL并将其用作合并的键

开始数据(已删除df_mentions中的空列)

print(df_mentions)
                                            Mentions        Date
0  https://www.lemonde.fr/football/article/2019/0...  08/07/2019
1  https://www.telegraph.co.uk/world-cup/2019/06/...  08/07/2019
2  https://www.washingtonpost.com/sports/dcunited...  08/07/2019

print(media)
            Source                             Link Country Foundation
0   New York times         https://www.nytimes.com/     USA       1851
1          Lemonde          https://www.lemonde.fr/  France       1944
2  Washington Post  https://www.washingtonpost.com/     USA       1877

创建一个包含基本网址的新列:

df_mentions['url'] = df_mentions['Mentions'].str.extract(r'(http[s]?:\/\/.+?\/)')

   Mentions                                   Date        url
0  https://www.lemonde.fr/football/articl...  08/07/2019  https://www.lemonde.fr/
1  https://www.telegraph.co.uk/world-cup/...  08/07/2019  https://www.telegraph.co.uk/
2  https://www.washingtonpost.com/sports/...  08/07/2019  https://www.washingtonpost.com/

在合并时使用该新列作为键:

df_mentions.merge(media,
                  left_on='url',
                  right_on='Link',
                  how='left').drop(columns=['url', 'Link'])

   Mentions                                Date        Source           Country Foundation
0  https://www.lemonde.fr/football/art...  08/07/2019  Lemonde          France  1944     
1  https://www.telegraph.co.uk/world-c...  08/07/2019  NaN              NaN     NaN      
2  https://www.washingtonpost.com/spor...  08/07/2019  Washington Post  USA     1877