[英]How to add a column from df1 to df2 if it not present in df2, else do nothing
I have 2 data frame from a basic web scrape using Pandas (below).我有 2 个来自基本 web 刮擦的数据框,使用 Pandas(下图)。 The second table has less columns than the first, and I need to concat the dataframes.第二个表的列比第一个少,我需要连接数据框。 I have been manually inserting columns for a while but seeing as they change frequently I would like to have a function that can assess the columns in df2, check whether they are all in df2, and if not, add the column, with the data from df2.我已经手动插入列有一段时间了,但是看到它们经常变化,我想要一个 function 可以评估 df2 中的列,检查它们是否都在 df2 中,如果没有,添加列,数据来自df2。
import pandas as pd
link = 'https://en.wikipedia.org/wiki/Opinion_polling_for_the_next_French_presidential_election'
df = pd.read_html(link,header=0)
df1 = df[1]
df1 = df1.drop([0])
df1 = df1.drop('Abs.',axis=1)
df2 = df[2]
df2 = df2.drop([0])
df2 = df2.drop(['Abs.'],axis=1)
Many thanks,非常感谢,
@divingTobi's answer: @divingTobi 的回答:
pd.concat([df1, df2])
does the trick. pd.concat([df1, df2])
可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.