【问题标题】:Comparing data frames with different size比较不同大小的数据帧
【发布时间】:2021-05-06 19:27:06
【问题描述】:

我正在比较两个不同大小的数据框。 (第一个数据框中的一列有 1000 个成员,我将它与另一个数据框中有 48 个成员的列进行比较),我收到了这个错误。

“只能比较标签相同的系列对象”

代码是:

df2['present_in_df1'] = np.where(df1['Names'] == df2['Names'], "yes", "no")

【问题讨论】:

标签: python dataframe compare


【解决方案1】:

numpy.where 与 isin()。

import pandas as pd
import numpy as np
name1=['A','B','C']
name2=['B','C']
df1=pd.DataFrame({'name':name1})
df2=pd.DataFrame({'name':name2})
#using numpy 
df2['present_in_df1'] = np.where(df2.name.isin(df1.name), 'Yes', 'No')
df2.head(3)

【讨论】:

    【解决方案2】:

    这有帮助吗?

    
    import pandas as pd
    d1 = {'a': [1,2,3,4,2,2,2]}
    d2 = {'b':[1,2,5,6]}
    
    df1 = pd.DataFrame(d1)
    df2 = pd.DataFrame(d2)
    
    df2['present_in_df1'] = df2['b'].isin(df1['a'])
    
    print(df2)
    
       b  present_in_df1
    0  1            True
    1  2            True
    2  5           False
    3  6           False
    [Finished in 0.38s]
    
    

    【讨论】:

      猜你喜欢
      • 2022-11-20
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 2021-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-23
      相关资源
      最近更新 更多