加盟产南

时间:2019-07-18 06:02:28

标签: python pandas

我想使用“技术”列上的表格进行合并。合并的结果为nan,而不是第二个表的值。

我尝试将用于合并的列转换为字符串,因子或整数,但问题仍然存在

import pandas as pd
multipliers = pd.read_csv("multipliers.csv")
multipliers.Technology.astype('str')
generators = pd.read_csv("generators.csv")
generators.Technology.astype('str')
mergeddf = generators.merge(multipliers, on = 'Technology', how = 'left')

generators.csv:

,idIntBus,idGen,idProfile,Technology,HedgeRatio,meanErrorForecast24h,sigmaErrorForecast24h,Pmax,Pmin,Qmax,Qmin,Emax,Emin,E_ini,VarCost,UnitType,CandidateUnit,Inc,FOC,IncE
0,1,1,15.0,wind,,0.0092,0.045,3000.0,0.0,480.0,-480.0,,,,0.0,NonDispatchable,1,100000.0,40000.0,
1,1,2,14.0,pv,,0.066,0.0,12000.0,0.0,480.0,-480.0,,,,0.0,NonDispatchable,1,100000.0,25000.0,

multipliers.csv:

,Technology,VC,FC
0,gas         ,1.0,1.0
1,wind        ,1.0,1.0
2,pv          ,1.0,1.0

我希望找到乘数表中的列是浮点数而不是nans。

2 个答案:

答案 0 :(得分:1)

我认为应该有一些空格或类似的空格,将两列都转换为列表进行测试:

print (multipliers.Technology.tolist())
print (generators.Technology.tolist())

因此可能的解决方案是Series.str.strip,如有必要,请将值转换为以下字符串:

multipliers.Technology= multipliers.Technology.astype('str').str.strip()
generators.Technology= generators.Technology.astype('str').str.strip()

mergeddf = generators.merge(multipliers, on = 'Technology', how = 'left')

对于测试,也只能使用默认的内部联接来匹配列的匹配值:

print(generators.merge(multipliers, on = 'Technology'))

答案 1 :(得分:1)

两件事:

  1. astype()返回一个数据框,而不是就地执行,因此将其更改为:

    multipliers['Technology'] = multipliers.Technology.astype('str')
    
  2. 使用strip(),因为有尾随空格

    generators['Technology'] = generators['Technology'].apply(lambda x: x.strip())
    

因此,您的代码将是:

import pandas as pd
multipliers = pd.read_csv("multipliers.csv")
multipliers['Technology'] = multipliers.Technology.astype('str')
generators = pd.read_csv("generators.csv")
generators['Technology'] = generators.Technology.astype('str')
generators['Technology'] = generators['Technology'].apply(lambda x: x.strip())
mergeddf = generators.merge(multipliers, on = 'Technology', how = 'left')