首页 > 解决方案 > 如何在python中的数据框列中添加一些计算

问题描述

输入excel表格

我有使用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 的每一列和每一行。我想要通过在列中进行计算来创建新的数据框

标签: pythonpandas

解决方案


试试下面的代码:

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)

希望这可以帮助!


推荐阅读