如何创建名为“reason”的列,显示哪些字符串匹配?
match_d = {"col_a":["green", "purple"], "col_b":["weak", "stro", "strong"],...}
df
fruit col_a col_b
0 apple yellow NaN
1 pear blue NaN
2 banana green strong
3 cherry green heavy
4 grapes brown light
...
预期产出
fruit col_a col_b reason
0 apple yellow NaN NaN
1 pear blue NaN NaN
2 banana green strong col_a:["green"], col_b:["stro", "strong"]
3 cherry green heavy col_a:["green"]
4 grapes brown light
1条答案
按热度按时间cl25kdpy1#
对
match_d
连接匹配值使用嵌套列表理解,如果不是空字符串,则使用列名连接值:.apply
的替代解决方案:编辑:要为
reason
列中未缺失的值添加子字符串,请使用: