python - 如何在python中的数据框列中添加一些计算
问题描述
我有使用pandas.read_excel的excel表,我在数据框中得到了输出,但我想在阅读pandas之后添加计算,我需要在每个x和y列中进行以下计算。
ratiox = (73.77481944859028 - 73.7709567323327) / 720
ratioy = (18.567453940477293 - 18.56167674097576) / 1184
mapLongitudeStart = 73.7709567323327
mapLatitudeStart = 18.567453940477293
longitude = 0, latitude = 0
longitude = (mapLongitudeStart + x1 * ratiox)) #I have take for the single column x1 value
latitude = (mapLatitudeStart - (-y1 *ratioy )) # taken column y1 value
如何将此计算应用于具有不应采用空值的值的 x 和 ya 的每一列和每一行。我想要通过在列中进行计算来创建新的数据框
解决方案
试试下面的代码:
import pandas as pd
import itertools
df = pd.read_excel('file_path')
dfx=df.ix[:,'x1'::2]
dfy=df.ix[:,'y1'::2]
li=[dfx.apply(lambda x:mapLongitudeStart + x * ratiox),dfy.apply(lambda y:mapLatitudeStart - (-y))]
df_new=pd.concat(li,axis=1)
df_new = df_new[list(itertools.chain(*zip(dfx.columns,dfy.columns)))]
print(df_new)
希望这可以帮助!
推荐阅读
- ios - 如何在 Swift 中从 Firestore 中读取多个自定义对象
- php - PHP计算数组并返回0而不是空
- python - Spark:加载多个文件,执行相同的操作并合并到一个数据帧中
- swift - SwiftUI 视图阻止了对 MapKit 的触摸,但不阻止其他视图
- c - C 编程:读取文件文本并尝试找出最长单词的问题
- json - Swift JSONDecode 解码数组/字典失败
- list - 列表作为可选构造函数参数在 Dart 中为空
- postgresql - docker-compose:在 cron 作业中转储 postgres 数据库不起作用
- r - 如何使用 ggplot 将一个自变量拆分为另一个以进行绘图
- delphi - 窗口在对接/脱离时松开手柄