我有2个csv命名为abc.csv和xyz.csv。我可以在Excel中使用VLOOKUP实现。想在Python中做。
abc.csv格式
"uid","isDisabled"
"user.0","active"
"user.1","Disabled"
"user.2","active"
"user.3","Disabled"
"user.4","active"
"user.5","Disabled"
"user.6","active"
"user.8","active"
xyz.csv格式
"uid","status"
"user.0","active"
"user.1","active"
"user.2","active"
"user.5","active"
"user.7","active"
寻找类似的结果。
输出.csv
"uid","status","abc_status"
"user.0","active","NOCHANGE"
"user.1","active","Disabled in ABC"
"user.2","active","NOCHANGE"
"user.5","active","Disabled in ABC"
"user.7","active","Does not exist in ABC"
这是可以实现的吗?
我用Excel试过了。没用Python。
2条答案
按热度按时间wbgh16ku1#
导入包:
读取CSV文件:
基于
uid
合并这些 Dataframe :定义检查状态的函数:
将
status()
函数应用于merged_df
的每一行:输出 Dataframe :
将
output_df
保存为CSV文件:output.csv
文件:rqdpfwrv2#
您可以使用csv模块完成此操作,方法是首先阅读ABC读入dict,将status键入uid:
abc状态Map如下所示:
您的示例输出没有涵盖XYZ中禁用但ABC中激活的情况。我继续编写代码,以解决这种可能性:
并修改了XYZ CSV来测试它:
创建最终行的列表,以将计算行追加到其中。使用输出标头初始化该列表:
然后读取XYZ,比较状态,追加计算出的最后几行,最后写入输出:
这就给了我: