Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
jorisvandenbossche on master
DOC: clean-up of v1.2.1 whatsne… (compare)
Florian
Hi there, can anyone help me with a seamingly simple task?
I have the following DataFrame:
Participant Time Value
0 1 1 1
1 1 2 2
2 1 3 3
3 2 1 2
4 2 2 5
5 2 3 7
And now I want to subtract the Value at Time==1 from the Value at Time==2 and Time==3, such that the DataFrame becomes:
Participant Time Value
0 1 1 0
1 1 2 1
2 1 3 2
3 2 1 0
4 2 2 3
5 2 3 5
I just cannot figure out how to do this :-)
kir0ul
> <@fladd:matrix.org> Hi there, can anyone help me with a seamingly simple task?
I have the following DataFrame:
Participant Time Value 0 1 1 1 1 1 2 2 2 1 3 3 3 2 1 2 4 2 2 5 5 2 3 7
And now I want to subtract the Value at Time==1 from the Value at Time==2 and Time==3, such that the DataFrame becomes:
Participant Time Value 0 1 1 0 1 1 2 1 2 1 3 2 3 2 1 0 4 2 2 3 5 2 3 5
I just cannot figure out how to do this :-)
Maybe this can help?
https://stackoverflow.com/questions/34855859/is-there-a-way-in-pandas-to-use-previous-row-value-in-dataframe-apply-when-previ
Florian
kir0ul: Thanks for the pointer. However, this does not seem to work. I do:
for i in range(1, len(data)):
time = data.loc[i, "Time"]
if time != 1:
data.loc[i, "Time"] = data.loc[i, "Time"] - data.loc[i-time-1, "Time"]
But I get the error:
KeyError: 'the label [-2] is not in the [index]'
Florian
ha, paranthesis error :-)
Florian
but it still does not work. I creates the original data frame, instead of the subtracted one
>>> from pandas import DataFrame as df
>>> import datetime
>>> inp = {'date':[datetime.datetime.now() for val in range(3)]}
>>> inp_tbl = df(inp)
>>> inp_tbl
date
0 2019-09-20 15:00:35.080488
1 2019-09-20 15:00:35.080525
2 2019-09-20 15:00:35.080527
>>> inp_tbl.dtypes
date datetime64[ns]
dtype: object
>>> inp_tbl['date'].values[0]
numpy.datetime64('2019-09-20T15:00:35.080488000')
.dt
accessor I thought it might be driven by some of the delegate
decorators and class variables like _datetimelike_methods
to_pydatetime
for a DTA)