【问题标题】:Python Pandas Match Vlookup columns based on header valuesPython Pandas 根据标题值匹配 Vlookup 列
【发布时间】:2017-07-19 17:44:00
【问题描述】:

我有以下数据框df:

Customer_ID | 2015 | 2016 |2017 | Year_joined_mailing
ABC            5      6     10     2015
BCD            6      7     3      2016        
DEF            10     4     5      2017
GHI            8      7     10     2016

我想查找客户在加入邮件列表的那一年的价值并将其保存在新列中。

输出将是:

Customer_ID | 2015 | 2016 |2017 | Year_joined_mailing | Purchases_1st_year
ABC            5      6     10     2015                       5
BCD            6      7     3      2016                       7       
DEF            10     4     5      2017                       5
GHI            8      9     10     2016                       9

我在 python 中找到了一些匹配 vlookup 的解决方案,但没有一个会使用其他列的标题。

【问题讨论】:

  • 查找是 2015、2016 和 2017 列

标签: python pandas match lookup


【解决方案1】:

弃用通知lookup 原为 deprecated in v1.2.0

使用pd.DataFrame.lookup
请记住,我假设Customer_ID 是索引。

df.lookup(df.index, df.Year_joined_mailing)

array([5, 7, 5, 7])

df.assign(
    Purchases_1st_year=df.lookup(df.index, df.Year_joined_mailing)
)

             2015  2016  2017  Year_joined_mailing  Purchases_1st_year
Customer_ID                                                           
ABC             5     6    10                 2015                   5
BCD             6     7     3                 2016                   7
DEF            10     4     5                 2017                   5
GHI             8     7    10                 2016                   7

但是,您必须小心比较列名中可能的字符串和第一年列中的整数...

确保遵守类型比较的核选项。

df.assign(
    Purchases_1st_year=df.rename(columns=str).lookup(
        df.index, df.Year_joined_mailing.astype(str)
    )
)

             2015  2016  2017  Year_joined_mailing  Purchases_1st_year
Customer_ID                                                           
ABC             5     6    10                 2015                   5
BCD             6     7     3                 2016                   7
DEF            10     4     5                 2017                   5
GHI             8     7    10                 2016                   7

【讨论】:

  • 魔术......没想到单线版是可能的 - 谢谢
  • 谢谢 - 它适用于一列,但对于同一数据框中的第二列,我收到此错误 KeyError: 'One or more column labels was not found' - 是否与索引有关?我将 Customer_ID 设置为索引 df = df.set_index(['Customer_ID'])
【解决方案2】:

您可以对每一行应用“应用”

df.apply(lambda x: x[x['Year_joined_mailing']],axis=1)

【讨论】:

    【解决方案3】:

    我会这样做,假设列标题和Year_joined_mailing 是相同的数据类型并且所有Year_joined_mailing 值都是有效列。如果数据类型不同,可以在适当的地方添加str()int() 进行转换。

    df['Purchases_1st_year'] = [df[df['Year_joined_mailing'][i]][i] for i in df.index]
    

    我们在这里所做的是遍历数据帧中的索引以获取该索引的 'Year_joined_mailing' 字段,然后使用它来获取我们想要的列,并再次从列中选择该索引,将其全部推送到一个列表并将其分配给我们的新列'Year_joined_mailing'

    如果您的 'Year_joined_mailing' 列并不总是有效的列名,请尝试:

    from numpy import nan
    new_col = []
    for i in df.index:
        try:
            new_col.append(df[df['Year_joined_mailing'][i]][i])
        except IndexError:
            new_col.append(nan) #or whatever null value you want here)
    df['Purchases_1st_year'] = new_col
    

    这个更长的代码 sn-p 完成同样的事情,但如果'Year_joined_mailing' 不在df.columns 中,则不会中断

    【讨论】:

    • 非常感谢 - 这也很有效;所以我赞成它
    猜你喜欢
    • 2021-09-10
    • 1970-01-01
    • 2021-07-22
    • 2021-01-02
    • 2016-11-22
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多