简体   繁体   中英

Compare Columns In Dataframe

I have two data frames that I have concatenated into one. What I ultimately want to end up with is a list of all the columns the exist in both. The data frames come from two different db tables, and I need to generate queries based on the ones that exist in both tables.

I tried doing the following: concat_per.query('doe_per==focus_per') but it returned an empty data frame.

      doe_per  focus_per
2         NaN  Period_02
3   Period_01  Period_06
4   Period_02  Period_08
5   Period_03        NaN
6   Period_04        NaN
7   Period_05        NaN
8   Period_06        NaN
9   Period_07        NaN
10  Period_08        NaN

also you can use function isin(). At first ,you can transform the first column to a set or list as you base columns. Then use isin() to filter the second dataframe.

    firstList = set(df1st.doe_per)
    targetDF = df2nd[df2nd.focus_per.isin(firstList)==True]

If you want to combine two dataframes into one, you can use

    pd.merge(df1,df2,left_on=df1st.doe_per,right_on = df2nd.focus_per,join='inner')

or

    pd.concat([df1,df2],on_,join='inner',ignore_index=True)

I'm sorry that i forgot some params in the function.But if you want to combine some dataframe into one, you need to use these two function. Maybe pd.combine() is ok. You can look up the api of pandas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM