mask and proper index
Peter Otten
__peter__ at web.de
Sun Feb 3 13:18:53 EST 2019
diego.avesani at gmail.com wrote:
> Dear all,
>> I am trying to apply a mask to my dataframe:
>> mask = (df['datatime'] > start_date) & (df['datatime'] <= end_date)
> df = df.loc[mask]
>>> It seems to work pretty well.
>> After that I crate the cumulative of its element as:
>>> PP_cumPP = np.cumsum(df[PP_station])
>>> However, I am not able to compute PP_cumPP last element. Indeed, when I do
>> len(PP_cumPP)
>> I get
>> 8783
>> and when I try to do:
>> PP_cumPP[len(PP_cumPP)-1]
>> I get an error.
The ultimate problem description ;) You can certainly do better than that.
Please do always try to make your example self-contained so that a reader
can run it and see the same error as you do.
At the very least cut and paste the traceback and the error message you are
getting.
> What I am doing wrong?
My steps to reproduce what is probably your problem:
>>> df = pd.DataFrame([[1],[2],[3]], columns=["foo"])
>>> odd = df.loc[df["foo"] & 1]
>>> odd
foo
0 1
2 3
[2 rows x 1 columns]
>>> odd["foo"][1]
Traceback (most recent call last):
[snip]
KeyError: 1
OK, let's try something else:
>>> odd["foo"][2]
3
It looks like you have to use the original indices. But google sure can find
a way to get rid of these:
>>> odd = odd.reset_index(drop=True)
>>> odd["foo"][1]
3
> Thanks a lot for any kind of help
>> Diedro
More information about the Python-list
mailing list