Skip to content
Advertisement

Tag: pandas

pandas subtracting value in another column from previous row

I have a dataframe (named df) sorted by identifier, id_number and contract_year_month in order like this so far: and would like to add a column named ‘date_difference’ that is consisted of contract_year_month minus collection_year_month from previous row based on identifier and id_number (e.g. 2018-01-08 minus 2018-01-09), so that the df would be: I already converted the type of contract_year_month and

Cannot convert the series to [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. This question does not appear to be about programming within the scope defined in the help center. Closed last year. The community reviewed whether to reopen this question 8 months ago and left it closed: Original close reason(s) were not resolved Improve this question I

Pandas: add column with progressive count of elements meeting a condition

Given the following dataframe df: I want to add another column that counts, progressively, the elements with df[‘B’]=’yes’: How can I do this? Answer You can use numpy.where with cumsum of boolean mask: Another solution is count boolean mask created by filtering and then add 0 values by reindex: Performance (in real data should be different, best check it first):

Alternative to the pandas negation operator

I’m trying to use the pandas negation operator ~ in one of my jinja2 templates but I believe its conflicting with their special operator ~. yields the following exception… I could do the operation on the python side and pass another variable with the negated selection but what’s the method name equivalent that the ~ operator maps to that I

return last date and value each month in pandas

I have a df in pandas with daily data. I would like to return the last value of each month. I thought the simple solution would be to .resample(“M”).apply(lambda ser: ser.iloc[-1,]) however, it seems as if resample actually computes the month end date rather than return the actual date that appears that month. Is this intended behavior? MWE: While the

Advertisement