如何使用条件将循环中公式化的新列分配给熊猫中的多个数据框?

时间:2019-04-09 08:55:32

标签: python python-3.x thomson-reuters-eikon refinitiv-eikon-api refinitiv-eikon

我有一个17个数据帧的列表:

CGdfs = [CGdf_2002,CGdf_2003,.......,CGdf_2018]

我需要向每个这些数据框添加一个名为“ custom_region”的新列。可以使用循环来做到这一点吗?

我尝试使用如下所示的for循环将该列分配给一个数据帧-CGdf_2002:

custom_region = []

#for df in CGdfs:
for region, country in zip(CGdf_2002['region_of_headquarters'], CGdf_2002['country_of_headquarters']):
  if(region == 'Americas' and country == 'United States of America'):
    custom_region.append('US')
  elif(region == 'Americas' and country != 'United States of America'):
    custom_region.append('Americas(ex-US)') 
  elif(region == 'Africa'):
    custom_region.append('Africa') 
  elif(region == 'Oceania'):
    custom_region.append('Oceania')
  elif(region == 'Europe' and country == 'United Kingdom'):
    custom_region.append('UK')
  elif(region == 'Europe' and country != 'United Kingdom'):
    custom_region.append('Europe(ex-UK)')
  elif(region == 'Asia' and country == 'Japan'):
    custom_region.append('Japan')
  elif(region == 'Asia' and country != 'Japan'):
    custom_region.append('Asia(ex-Japan)')
  else:
    custom_region.append('')

CGdf_2002['custom_region'] = custom_region

我想对上面列表中的所有数据帧一起执行上述操作。请提出解决方案。

0 个答案:

没有答案