gpt4 book ai didi

python - 将函数应用于将另一列作为 Python Pandas 中的参数的列

转载 作者:行者123 更新时间:2023-11-28 17:28:45 25 4
gpt4 key购买 nike

我想将函数应用于 Pandas 数据框中的整个列。此函数将覆盖当前在该列中的数据,但需要它旁边的另一列的值,以说明:

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'>

错误意味着我将整数列传递给函数,而不是逐行传递。我将如何做到这一点?

最佳答案

IIUC 你需要iloc用于选择第二列并添加 lambda,如前所述 EdChum :

def retrieve_original_string(x):
x = x + 4
#add code
return x


df.iloc[:,1] = df.apply(lambda x: retrieve_original_string(x[0]), axis=1)
print df
col 0 col 1
0 23 27
1 45 49
2 63 67

#if you need new column
df['a'] = df.apply(lambda x: retrieve_original_string(x[0]), axis=1)
print df
col 0 col 1 a
0 23 'word' 27
1 45 'word2' 49
2 63 'word3' 67

或者:

def retrieve_original_string(x):
x = x + 4
#add code
return x


df.iloc[:,1] = df.iloc[:,0].apply(retrieve_original_string)
print df
col 0 col 1
0 23 27
1 45 49
2 63 67

关于python - 将函数应用于将另一列作为 Python Pandas 中的参数的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36306754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com