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
i.e. 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:
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