Ale*_*lex 5 python numpy pow pandas
有没有一种pythonic方法可以将DataFrame(xRaw
)中的列提升为连续的幂?是否有类似的东西
xRaw[:,k] = xRaw.pow(k) for k in range(1,6)
Run Code Online (Sandbox Code Playgroud)
raf*_*elc 10
这是一个Vandermonde矩阵,其中numpy具有内置函数 np.vander
如果你有
s = pd.Series([1,2,3,4,5])
Run Code Online (Sandbox Code Playgroud)
然后
np.vander(s, 6)
Run Code Online (Sandbox Code Playgroud)
array([[ 1, 1, 1, 1, 1, 1],
[ 1, 2, 4, 8, 16, 32],
[ 1, 3, 9, 27, 81, 243],
[ 1, 4, 16, 64, 256, 1024],
[ 1, 5, 25, 125, 625, 3125]])
Run Code Online (Sandbox Code Playgroud)
要添加回df
,您可以使用concat
df = pd.concat([df, pd.DataFrame(vander)], axis=1)
Run Code Online (Sandbox Code Playgroud)
麻木广播
s.values**np.arange(6)[:,None]
Out[70]:
array([[ 1, 1, 1, 1, 1],
[ 2, 4, 8, 16, 32],
[ 3, 9, 27, 81, 243],
[ 4, 16, 64, 256, 1024],
[ 5, 25, 125, 625, 3125]], dtype=int64)
Run Code Online (Sandbox Code Playgroud)
重新分配
s= pd.concat([s, pd.DataFrame(s.values**np.arange(6)[:,None],index=s.index)], axis=1)
Run Code Online (Sandbox Code Playgroud)
我会做:
for i in range(6):
xRaw["power: "+str(i)] = xRaw[column_to_be_raised] ** i
Run Code Online (Sandbox Code Playgroud)
这将生成一个新列,该列的基数取自您想要为给定范围内的每个指数求出的列。
归档时间: |
|
查看次数: |
105 次 |
最近记录: |