我正在尝试将函数应用于pandas数据帧,这样的函数需要两个np.array作为输入,并且它使用定义良好的模型来拟合它们.
关键是我无法从所选列开始应用此函数,因为它们的"行"包含从JSON文件而不是np.array读取的列表.
现在,我尝试了不同的解决方案:
#Here is where I discover the problem
train_df['result'] = train_df.apply(my_function(train_df['col1'],train_df['col2']))
#so I've tried to cast the Series before passing them to the function in both these ways:
X_col1_casted = trai_df['col1'].dtype(np.array)
X_col2_casted = trai_df['col2'].dtype(np.array)
Run Code Online (Sandbox Code Playgroud)
不起作用.
X_col1_casted = trai_df['col1'].astype(np.array)
X_col2_casted = trai_df['col2'].astype(np.array)
Run Code Online (Sandbox Code Playgroud)
不起作用.
X_col1_casted = trai_df['col1'].dtype(np.array)
X_col2_casted = trai_df['col2'].dtype(np.array)
Run Code Online (Sandbox Code Playgroud)
不行.
我现在想做的是一个很长的过程,如:
从未发布的列系列开始,将它们转换为list(),迭代它们将函数应用于np.array()单个元素,并将结果附加到临时列表中.完成后,我将此列表转换为新列.(显然,我不知道它是否会起作用)
你们中的任何人都知道如何帮助我吗?
编辑:我添加一个例子清楚:
该函数假定输入两个np.arrays.现在它有两个列表,因为它们是从json文件中检索的.情况是这样的:
col1 col2 result
[1,2,3] [4,5,6] [5,7,9]
[0,0,0] [1,2,3] [1,2,3]
Run Code Online (Sandbox Code Playgroud)
显然,功能不是总和,而是自己的功能.假设这个总和只能从数组而不是表单开始工作,我该怎么办?
提前致谢