英文:
DataFrame rolling with complex function
问题
以下是我的伪代码:
import pandas as pd
import numpy as np
dict = {'a': [1,2,3,4,5,6,7,8],
'b': [2,3,4,5,6,7,8,9],
'c': [3,4,5,6,7,8,9,10]
}
df = pd.DataFrame.from_dict(dict)
def make_coeff(x):
a_new = x[0]
b_new = x[1]
c_new = x[2]
a_new.insert(0,1)
b_new[0] = 1
c_new[0] = 1
return array_value(a_new, b_new, c_new)
df['array_col'] = df.rolling(3)[['a','b','c']].apply(make_coeff)
def array_value(a, b, c):
tem = np.tensordot(a,b, axis=0)
tem = np.tensordot(tem,c, axis=0)
return tem
希望这能帮助您创建新列 array_col
,根据滚动窗口执行相应的操作。
英文:
Below is my quasi-code:
import pandas as pd
dict = {'a': [1,2,3,4,5,6,7,8],
'b': [2,3,4,5,6,7,8,9],
'c': [3,4,5,6,7,8,9,10]
}
df = pd.DataFrame.from_dict(dict)
def make_coeff(x):
a_new = x[0]
b_new = x[1]
c_new = x[2]
a_new.insert(0,1)
b_new[0] = 1
c_new[0] = 1
return array_value(a_new, b_new, c_new)
df['array_col'] = df.rolling(3)[['a','b','c']].apply(make_coeff)
I want to create new col array_col
depending on the rolling window with following actions:
insert 1 to a at beginning; change b[0] = c[0] = 1
use row 2 as an example:
row 2: a = [3,2,1], b = [4,3,2], c = [5,4,3] # 3 rolling window from row 2
row 2: a_new = [1,3,2,1], b_new = [1,3,2], c_new = [1,4,3] # action on each column
row 2 of array_col
: array_value(a_new, b_new, c_new) # return a array value
here array_value
is a known function returning an multi-dimensional array value. Let's assume it is a tensor product between all vectors with shape (len(a), len(b), len(c))
:
def array_value(a, b ,c):
tem = np.tensordot(a,b, axis=0)
tem = np.tensordot(tem,c, axis=0)
return tem
答案1
得分: 1
以下是代码的中文翻译:
import pandas as pd
import numpy as np
# 创建一个字典
dict = {'a': [1,2,3,4,5,6,7,8],
'b': [2,3,4,5,6,7,8,9],
'c': [3,4,5,6,7,8,9,10]
}
# 将字典转换为DataFrame
df = pd.DataFrame.from_dict(dict)
# 创建一个数组列,包含滚动窗口中的列a的值
df['a_rolling'] = [window.to_list() for window in df['a'].rolling(window=3)]
df['b_rolling'] = [window.to_list() for window in df['b'].rolling(window=3)]
df['c_rolling'] = [window.to_list() for window in df['c'].rolling(window=3)]
# 定义一个函数用于计算系数
def make_coeff(a, b, c):
if len(a) < 3:
return [0,0,0] # 显然前几行没有足够的值
# 在a的开头插入0,1
a = np.insert(a, 0, 0)
b[0] = 1
c[0] = 1
return array_value(a, b, c)
# 计算数组值的函数
def array_value(a, b, c):
tem = np.tensordot(a, b, axes=0)
tem = np.tensordot(tem, c, axes=0)
return tem
# 应用函数以创建系数列
df['coeff'] = df.apply(lambda x: make_coeff(x['a_rolling'], x['b_rolling'], x['c_rolling']), axis=1)
这是代码的翻译部分,没有其他内容。
英文:
import pandas as pd
import numpy as np
dict = {'a': [1,2,3,4,5,6,7,8],
'b': [2,3,4,5,6,7,8,9],
'c': [3,4,5,6,7,8,9,10]
}
df = pd.DataFrame.from_dict(dict)
# create an array column of the rolling values of column a
df['a_rolling'] = [window.to_list() for window in df['a'].rolling(window=3)]
df['b_rolling'] = [window.to_list() for window in df['b'].rolling(window=3)]
df['c_rolling'] = [window.to_list() for window in df['c'].rolling(window=3)]
def make_coeff(a, b, c):
if len(a) < 3:
return [0,0,0] # the first couple of rows obviously don't have enough values
# insert 0,1 at the beginning of a
a = np.insert(a, 0, 0)
b[0] = 1
c[0] = 1
return array_value(a, b, c)
def array_value(a, b ,c):
tem = np.tensordot(a, b, axes=0)
tem = np.tensordot(tem, c, axes=0)
return tem
df['coeff'] = df.apply(lambda x: make_coeff(x['a_rolling'], x['b_rolling'], x['c_rolling']), axis=1)
This is likely not precisely what you want, but it is something
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论