python - 如何使用列值计算方程并使用python将输出值存储在另一个之下?
问题描述
我有一个数据集,其列值将用于方程式。
Max angle
是用户定义的,角度增量Ang
将是角度步长。
假设 Max Angle = 30 , Angular Increment = 10,所以我想要每个输入行有 4 个输出行。只有角度必须随方程中的 0、10、20、30 变化。
第一列是我的索引“ID”。我的数据集由 300 行组成。所以我的最终输出必须有 300*4(角度步长)行。
样本数据集:
编辑数据集:
data ='''
ID,1,2,3
23,0.88905321,0.500807892,0.499545029
105,0.334209544,0.24077062,0.345252261
47,0.020669404,0.154582048,0.044395524
28,0.07913145,0.987645061,0.421184162
23,0.5654544,0.879541062,0.456556261
105,0.45678404,0.789546214,0.456217524
import pandas as pd
import numpy as np
from math import *
data ='''
ID,1,2,3
23,0.88905321,0.500807892,0.499545029
105,0.334209544,0.24077062,0.345252261
47,0.020669404,0.154582048,0.044395524
28,0.07913145,0.987645061,0.421184162
'''
df = pd.read_csv(io.StringIO(data),index_col=0)
M = df.iloc[:,:]
#suppose
Max_ang = 30
Ang = 10
#Equation:
solution = 0.88905321*cos(Ang*(pi/180)) + 0.500807892*sin(Ang*(pi/180)) + 0.499545029 * sin(Ang*(pi/180))*cos(Ang*(pi/180))
方程:
解 = Column1_val x cos(Ang x (pi/180)) + Column2_val x sin(Ang x (pi/180)) + Column3_val x sin(Ang x (pi/180)) x cos(Ang x (pi/180) )
预期输出:
解决方案
这里不需要for loop
with iterrows
,这会很慢。
这是一个使用numpy broadcasting
.
reindex
首先,我们使用and获取正确格式的数据框index.repeat
:
import numpy as np
Max_ang = 30
Ang = 10
Angels = np.arange(0,Max_ang+Ang,step=Ang).tolist()
df = df.reindex(df.index.repeat(len(Angels)))
df['Ang'] = Angels * df.index.nunique()
pi_div_180 = np.pi/180
df['new'] = \
df['1'] * np.cos(df['Ang'] * pi_div_180) + \
df['2'] * np.sin(df['Ang'] * pi_div_180) + \
df['3'] * np.sin(df['Ang'] * pi_div_180) * np.cos(df['Ang']*pi_div_180)
输出
1 2 3 Ang new
ID
23 0.889053 0.500808 0.499545 0 0.889053
23 0.889053 0.500808 0.499545 10 1.047938
23 0.889053 0.500808 0.499545 20 1.167274
23 0.889053 0.500808 0.499545 30 1.236656
105 0.334210 0.240771 0.345252 0 0.334210
105 0.334210 0.240771 0.345252 10 0.429983
105 0.334210 0.240771 0.345252 20 0.507365
105 0.334210 0.240771 0.345252 30 0.559318
47 0.020669 0.154582 0.044396 0 0.020669
47 0.020669 0.154582 0.044396 10 0.054790
47 0.020669 0.154582 0.044396 20 0.086562
47 0.020669 0.154582 0.044396 30 0.114415
28 0.079131 0.987645 0.421184 0 0.079131
28 0.079131 0.987645 0.421184 10 0.321459
28 0.079131 0.987645 0.421184 20 0.547520
28 0.079131 0.987645 0.421184 30 0.744730
要删除不必要的列,请使用df.filter
:
df = df.filter(regex='\D')
Ang new
ID
23 0 0.889053
23 10 1.047938
23 20 1.167274
23 30 1.236656
105 0 0.334210
105 10 0.429983
105 20 0.507365
105 30 0.559318
47 0 0.020669
47 10 0.054790
47 20 0.086562
47 30 0.114415
28 0 0.079131
28 10 0.321459
28 20 0.547520
28 30 0.744730
推荐阅读
- linux - 如何在 Centos 7 中将 libtiff 更新到最新版本?
- python - While循环猜数
- python - 不能使用评估的计算器赋值。它给出了类型错误:“NoneType”对象在最后一个函数中不可迭代
- android - 使用来自 android studio 上 SQLite 的值更新 mysql Row
- docker - docker中的集群管理和编排有什么区别?
- azure - 用于查找给定订阅和区域的可用 VM 大小/sku 的 Azure ARM API
- android - BeaconManager updateScanPeriods 不会改变扫描率
- python - 如何使用 python 在运行时验证两个 JSON API 响应键值对匹配?
- ruby-on-rails - wicked_pdf,无法呈现 pdf
- node.js - 节点,创建一个流对象,内部已经有一些流管道