【问题标题】:Pandas compare Object columns熊猫比较对象列
【发布时间】:2021-10-10 14:36:01
【问题描述】:

我有两个对象列,其中包含我从 2 个 CSV 文件在熊猫上创建的数字列表。 我想在其中两个之间进行比较并添加一个新列,该列将为我提供相同数字的数量。

例如: 表格1: Numbers to compare

表 2: Data numbers according to date

我真正感兴趣的是表 2 中每个日期的表 1 中任何数字的实际比较。

The desired result, adding a column, which indicates how many numbers exist in the table of the given number

非常感谢

【问题讨论】:

  • 请发布代码以重现这些表格,而不是发布图像
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: python pandas compare


【解决方案1】:

表1:

df1 = pd.DataFrame({
    'a':1, 'b':2, 'c':3, 'd':4
}, index=range(1))

表2:

df2 = pd.DataFrame({
    'a':[1, 2, 2, 2], 
    'b':[2, 4, 3, 1],
    'c':[3, 5, 6, 3], 
    'd':[4, 7, 7, 5],
}, index=['01/01/2021', '02/01/2021', '03/02/2021', '04/02/2021'])

想要的结果:

df2['no of numbers'] = [
    list(row.isin(df1.values[0])).count(True) \
    for index, row in df2.iterrows()
]

它应该可以工作。

【讨论】:

    猜你喜欢
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2014-10-08
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 2019-09-17
    相关资源
    最近更新 更多