[英]python while loop to combine and delete repeated rows
I got a dataframe like: 我得到了一个像这样的数据框:
Type: Volume: Date: Price:....
Q 10 2016.6.1 10
Q 20 2016.6.1 20
T 10 2016.6.2
Q 10 2016.6.3
T 20 2016.6.4
T 20 2016.6.5
Q 10 2016.6.6
and I want to add up the value of 'volume' only if two(or more) Ts are consecutive and delete one of the row 并且我只想在两个(或多个)T连续的情况下将'volume'的值相加,并删除该行之一
ie to : 即:
Q 10 2016.6.1
Q 20 2016.6.1
T 10 2016.6.2
Q 10 2016.6.3
T 20+20=40 2016.6.4
Q 10 2016.6.6
now I'm using a if loop: 现在我正在使用一个if循环:
l = len(df)
Volume = df['Volume']
Type = df['Type']
for i in range(2,l-1):
if Type[i] == 'Trade':
if Type[i] == 'Trade' and Type[i+1] == 'Trade' :
Volume[i] = Volume[i]+Volume[i+1]
df = np.delete(fd, (i), axis=0)
However, I am getting an error: 但是,我得到一个错误:
ValueError: Shape of passed values is (8, 303540), indices imply (8, 303541)
Also, I would like to change the 'if' loop to a 'while' loop so I can handle data more easily if there are more than two consecutive type 'Trade' data 另外,我想将'if'循环更改为'while'循环,这样,如果有两个以上连续的'Trade'数据类型,我可以更轻松地处理数据
If you want to edit an iterable while looping over it, it's generally safer to work on a copy of the data inside the loop and replace the original with that updated copy afterwards. 如果要在循环时编辑一个可迭代的对象,通常更安全的方法是在循环中处理数据的副本,然后用该更新的副本替换原始对象。 This avoids Python getting confused about its position in the iteration (which is the problem that seems hinted at in your error, as it complains about indices).
这样可以避免Python对其在迭代中的位置感到困惑(这是您的错误中似乎暗示的问题,因为它抱怨索引)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.