[在excel文件上使用python的简单操作

问题描述 投票:0回答:1

x,y,粒子

23,25,0

12,15,0

54,45,0

32,11,1

21,43,1

43,11,1

在Excel中有3列的产量。第一列x,第二列y,第三列ID。如果粒子具有相同的标识号(ID),我想减去具有相同ID号的粒子的x和y。例如;

对于ID = 0的列,我希望它:

对于x应该是23-12 = 1111-54 = -43

y应该是

25-15 = 10

10-45 = -35

这是我为此编写的代码。第一个对于相同的ID号可以正常工作,但是在第二个周期中出现范围错误。可能是什么问题呢。我该如何解决。

from pyexcel_ods import get_data,save_data

# -*- coding: utf-8 -*-

data = get_data("deneme.ods")

s,extract=0,0

for i in range(len(data[u'Sheet1'])-2):
if data[u'Sheet1'][i][2]==data[u'Sheet1'][i+1][2]:
    s+=1
    continue
else:
    for j in range(s+1):
        extract -= data[u'Sheet1'][j][1]
        extract = abs(extract)
    data[u'Sheet1'][i].append(extract)
    extract=0
    s=0

save_data('deneme1.ods',data)

python excel subtraction operation pyexcel
1个回答
0
投票
import pandas as pd from operator import sub from functools import reduce d = [["x", "y", "particle"], [23, 25, 0],[12, 15, 0], [54, 45, 0], [32, 11, 1], [21, 43, 1], [43, 11, 1]] df = pd.DataFrame(d[1:], columns=d[0]) my_sub = lambda e: reduce(sub, e) df2 = df.pivot_table(index = "particle", values=["x", "y"], aggfunc=my_sub) print(df2)
© www.soinside.com 2019 - 2024. All rights reserved.