我正在尝试对数据帧中的一列运行函数(row_extract),该函数返回三个值,然后我想将它们添加到三个新列中。
我试过这样运行它
all_data["substance", "extracted name", "name confidence"] = all_data["name"].apply(row_extract)
但是我得到一个包含所有三个值的列。我将遍历行,但这似乎不是一个非常有效的系统-有什么想法吗?
这是我目前的解决方案,但它需要一段时间。
for index, row in all_data.iterrows():
all_data.at[index, "substance"], all_data.at[index, "extracted name"], all_data.at[index, "name confidence"] = row_extract(row["name"])
发布于 2020-04-23 11:27:24
检查函数输出的类型或数据类型。这看起来像是一个字符串。
您可以在字符串上使用"split“方法来分隔它们。
https://docs.python.org/2/library/string.html https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.split.html
或者,调整您的函数以返回多个值。
例如。
def myfunc():
...
...
return x, y, z
https://stackoverflow.com/questions/61385471
复制相似问题