Skip to content
Advertisement

Pandas: groupby().apply() custom function when groups variables aren’t the same length?

I have a large dataset of over 2M rows with the following structure:

JavaScript

If I wanted to calculate the net debt for each person at each month I would do this:

JavaScript

However the result is full of NA values, which I believe is a result of the dataframe not having the same amount of cash and debt variables for each person and month. Is there a way for me to avoid this and simply get the net debt for each month/person when possible and an NA for when it’s not?

Also, I’m kind of new to python and as I mentioned the dataset on which I’m working on is pretty large – so if anyone know a quicker/alternative method for this it would be greatly appreciated!

Advertisement

Answer

IIUC, use loc:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement