Skip to content

Tag: pandas

Getting Rolling Sum per Group

I have a dataframe like this: I would like to get the Sum of the last three months (excluding the current month), per Product_ID. Therefore I tried this: My code is failing, because it does not only calculate it per product, but it will give me also numbers for other products (let’s say Product 2, quart…

XLWINGS conditional formatting based on value

I have a dataframe in Pandas that I have exported to Excel and formatted headers etc. using XLWINGS. That works like a charm. But I would like to format two columns based on their value in each cell (row), so if the value is above 0 then green and below zero then red. I can’t seem to find that option

How to import API data using Pandas?

I am trying to pull some data from EIA API, below is what I tried but I’m getting the error on the first line of code: AttributeError: ‘str’ object has no attribute ‘text’ Any help would be much appreciated! Answer You haven’t requested anything from the API. Look carefully…

Pandas groupby – Find mean of first 10 items

I have 30 items in each group. To find mean of entire items, I use this code. That returns a value like this. However, I would like to find the mean of the first 10 items in the group instead of the entire items. That code return only a single Value instead of a pandas series. So I’m getting errors

How to do line level logic in Pandas

I have a table that has a bunch of columns and I need to create a new column based on the row type but the logic will be different for each type of row. My data looks like this: type field1 field2 field3 field4 1 a b c 17 2 e f g 20 3 i j k 100 the