我希望将一个函数应用到Pandas dataframe中的整个列。该函数将覆盖该列中当前的数据,但需要该列旁边的另一列的值来完成操作,以此举例说明:
col 0, col 1,
23, 'word'
45, 'word2'
63, 'word3'
我尝试将数字列传递到Pandas apply方法中:
df[1] = df.apply(retrieve_original_string(df[0]), axis=1)
但是这会报错:
sys:1: DtypeWarning: Columns (3,4) have mixed types. Specify dtype option on import or set low_memory=False.
Traceback (most recent call last):
File "/home/noname365/similar_keywords_microsoft/similar_keywords.py", line 95, in <module>
merged_df[1] = merged_df.apply(retrieve_original_string(merged_df[0], match_df), axis=1)
File "/home/noname365/similar_keywords_microsoft/similar_keywords.py", line 12, in retrieve_original_string
row_num = int(row)
File "/home/noname365/virtualenvs/env35/lib/python3.5/site-packages/pandas/core/series.py", line 81, in wrapper
"cannot convert the series to {0}".format(str(converter)))
TypeError: cannot convert the series to <class 'int'>
这个错误意味着我将整个数字列传递给函数,而不是逐行单独处理。我该如何解决这个问题?
df[1] = df.apply(lambda row: retrieve_original_string(row[0]), axis=1)
。 - EdChum