【问题标题】:How to add some calculation in columns of the dataframe in python如何在python中的数据框列中添加一些计算
【发布时间】:2019-09-27 18:36:55
【问题描述】:

我有使用 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 和 y a 的每一列和每一行,其中的值不应为空值。我想要通过在列中进行计算来创建新的数据框

【问题讨论】:

  • 您能否以文本形式提供来自pandas Dataframe 的一些示例行?
  • Pune पुणे हवेली बाणेर 3 ROAD WHITE #FFFFFF 11803.84 -3546.78 11808.36 -3531.79 11812.89 -3523.84 11830.37 -3523.84 11854.1 -3545.22 11854.1 -3548.58 11890.25 -3566.37 11942.91 -3577.78 11970.66 -3587.68 12005.36 -3587.68 12018.9 -3582.07 12033.11 -3582.07 12074.74 -3569.53 12066.81 -3603.85 12050.64 -3689.78 12046.44 -3712.12 12038.74 -3725.88 12034.46 -3769.25 12027.85 -3791.69 12005.81 -3791.68 12045.49 -3625.73 12039.1 -3609.91 12009.37 -3621.93 11971.88 -3621.93 11942.31 -3608.22 11888.45 -3596.61 11850.94 -3583.88跨度>
  • 以上是文本形式的单行数据
  • 你应该edit这个问题包含数据,而不是在评论中添加它
  • 您也应该添加列标题,也许您可​​以使用更少的列来解释问题。

标签: python pandas


【解决方案1】:

试试下面的代码:

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)

希望这会有所帮助!

【讨论】:

  • 我希望列应该是 x 和 y 格式意味着列,例如 x1 ,y1,x2,y2 等等。你能建议一下吗
  • @P.D 请检查编辑后的答案。它应该工作!
  • 我收到此错误 [NameError: name 'itertools' is not defined ] 我还从 itertools 导入链导入
  • 当我只使用 {import itertools} 时不会出现错误,但输出是以以前的方式获得的,而不是 x 和 y 列形式
  • 打印 df_new 将为您提供 x 和 y 列的明智形式。请检查!
【解决方案2】:

我首先建议将您的数据重新整形为长格式,这样您就可以自然地摆脱空单元格。此外,大多数 pandas 函数这样工作得更好,因为这样你就可以在所有 x 或 y 或 wahtever 维度上使用 group by 操作之类的东西

from itertools import chain
import pandas as pd

## this part is only to have a running example 
## here you would load your excel file
D = pd.DataFrame( 
        np.random.randn(10,6), 
        columns =chain(*[ [f"x{i}", f"y{i}"]  for i in range(1,4)])
    ) 

D["rowid"] = pd.np.arange(len(D))
D = D.melt(id_vars="rowid").dropna()
D["varIndex"] = D.variable.str[1]
D["variable"] = D.variable.str[0]

D = D.set_index(["varIndex","rowid","variable"])\
    .unstack("variable")\
    .droplevel(0, axis=1)

因此,这些转换将为您提供一个表,在该表中,您既有原始行 id(可能是时间序列或其他)的索引,也有变量索引 x1 或 x 2

现在您可以通过越冬之前的列来进行计算

## Everything here is a constant
ratiox = (73.77481944859028 - 73.7709567323327) / 720
ratioy = (18.567453940477293 - 18.56167674097576) / 1184
mapLongitudeStart = 73.7709567323327
mapLatitudeStart = 18.567453940477293

# apply the calculations directly to the columns
D.x = (mapLongitudeStart + D.x * ratiox))
D.y = (mapLatitudeStart - (-D.y * ratioy ))

【讨论】:

  • 我想在每个 x 和 y 列中添加这个计算
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-29
  • 2017-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-11
相关资源
最近更新 更多