我有带有“Indicator_Name”字段的数据框,其中包含一些文本,我正在应用一些返回 2 个值并需要在数据框中更新相同值的函数函数“String_Year_Pick_V2”返回两个值,如“XYZ”、“ABC”,需要针对每个“Indicator_Name”添加这些值我试过下面的脚本:DataT[['Indicator 2', 'Year']] = DataT['Indicator_Name'].apply(String_Year_Pick_V2, axis=1)Error:TypeError: String_Year_Pick_V2() got an unexpected keyword argument 'axis'DataT[['Indicator 2', 'Year']] = DataT['Indicator_Name'].apply(String_Year_Pick_V2)Error: ValueError: shape mismatch: value array of shape (252,) could not be broadcast to indexing result of shape (2,252脚本:DataT['Indicator_Name'].apply(String_Year_Pick_V2) - 给出结果DataT[['Indicator 2', 'Year']] = DataT.apply(lambda Row: String_Year_Pick_V2(Row['Indicator_Name']), axis=1)Error: ValueError: shape mismatch: value array of shape (252,) could not be broadcast to indexing result of shape (2,252)
1 回答
MMMHUHU
TA贡献1834条经验 获得超8个赞
您可以Series在函数的输出中创建:
def String_Year_Pick_V2(x):
#your code
year = x + 2000
ind = x + 10
return pd.Series([ind, year])
DataT = pd.DataFrame({'Indicator_Name':[1,2,3]})
DataT[['Indicator 2', 'Year']] = DataT['Indicator_Name'].apply(String_Year_Pick_V2)
print (DataT)
Indicator_Name Indicator 2 Year
0 1 11 2001
1 2 12 2002
2 3 13 2003
添加回答
举报
0/150
提交
取消