繁体   English   中英

我有两个数据框。 我想将一个数据框的标题与另一数据框的一列的内容进行比较

[英]I have two dataframes. I wanted to compare header of one dataframe with the content of one column in another dataframe

我有两个数据框。 在第一个数据帧中,有一个列名testname,其中我有88个测试,在第二个数据帧中,我将那些测试名作为标题。 因此,现在我想将第一个数据帧中的testname列的内容与第二个数据帧的标头进行比较。 我想检查第一个数据框中的每个id是否可以使用testname,我想在第二个数据框中添加特定的testname值。

我已经通过迭代两个数据帧来进行尝试,并且第一步也完成了匹配ID的操作,但是我不知道如何同时比较内容和标头

根据我对问题陈述的理解,遵循我的解决方案:

import pandas as pd

#Creating Data Frames
df1 = pd.DataFrame({'testname':['88test', 'nonono', '88test'],
               'col2': [1, 2, 3],
               'col3': [4, 5, 6]})
df2 = pd.DataFrame({'88test':[9, 10], 'col4':[11, 12]})

#Selecting rows and column of interest from df1
df1_selected_rows = df1[['testname']][df1['testname']=='88test']
df1_selected_rows.rename(columns={'testname':'88test'}, inplace=True)

#Merging data of interest into df2 using pandas.merge()
pd.merge(df2, df1_selected_rows, how='outer')

#The same can be achieved using pandas.concat(), but preserving the original indexes of both dataframes
pd.concat(objs=[df2,df1_selected_rows], sort=False)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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