Python Pandas 根据 header 值匹配 Vlookup 列
Python Pandas Match Vlookup columns based on header values
我有以下数据框 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 的一些解决方案,但是 none 会使用其他列的 headers。
Deprecation Notice: lookup
was 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
您可以对每一行应用 "apply"
df.apply(lambda x: x[x['Year_joined_mailing']],axis=1)
我会这样做,假设列 headers 和 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
这段较长的代码片段完成了同样的事情,但如果 'Year_joined_mailing'
不在 df.columns
中则不会中断
我有以下数据框 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 的一些解决方案,但是 none 会使用其他列的 headers。
Deprecation Notice:
lookup
was 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
您可以对每一行应用 "apply"
df.apply(lambda x: x[x['Year_joined_mailing']],axis=1)
我会这样做,假设列 headers 和 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
这段较长的代码片段完成了同样的事情,但如果 'Year_joined_mailing'
不在 df.columns