0

Each hour I get current weather data via an API. Current weather data is appended to the bottom of the dataframe:

df = df.append([current_weather], sort=False, ignore_index=True)

Current weather data includes precipitation totals for the past hour (precipitation_1h).

I also have a column for 24 hour precipitation totals (precipitation_1d). I want to calculate this value for the appended row only, not the entire column (so I'm not looking to use df.rolling).

Here's what I've tried...

This code skips the bottom row (i.e. the most recent precipitation_1h value):

df.at[current_weather['timestamp'], 'precipitation_1d'] = df.iloc[-1:-25 , df.columns.get_loc('precipitation_1h')].sum()

This code includes too many rows (I think all rows are included and bottom 25 counted twice):

df.at[current_weather['timestamp'], 'precipitation_1d'] = df.iloc[0:-25 , df.columns.get_loc('precipitation_1h')].sum()

This code works if I flip the order and add the new data as the first row of the dataframe (but I'd prefer to keep the new data at the bottom of the dataframe):

df.at[current_weather['timestamp'], 'precipitation_1d'] = df.iloc[:24 , df.columns.get_loc('precipitation_1h')].sum()

Any ideas?

UPDATE: G. Anderson's suggestion in the comments worked perfectly. He said it'd be helpful to see dataframe so I've included a screenshot of the tail in case it helps anyone in the future. Tail screenshot shows the dataframe after I've applied G. Anderson's solution. Before, all columns ending in 1d, 5d, 10d, and 20d had NaN values. Now they have sums (precipitation) or means (humidity, temp).

dataframe screenshot

tjc4
  • 191
  • 2
  • 7
  • It would help to see samples of your input and output data to better understand your problem, but my first instinct is to use something like `df.tail(24)['precipitation_1h'].sum()` – G. Anderson Mar 04 '20 at 23:21
  • G. Anderson your solution worked great (and code is much more succinct than what I was messing with). Thank you. I've added a screenshot to help show data samples as suggested in the hopes that they might help someone in the future. – tjc4 Mar 04 '20 at 23:42
  • 1
    Does this answer your question? [How to get the last N rows of a pandas DataFrame?](https://stackoverflow.com/questions/14663004/how-to-get-the-last-n-rows-of-a-pandas-dataframe) – AMC Mar 05 '20 at 01:30

0 Answers0