score:0
import itertools
>>> index = list(itertools.product(df['Date'].unique(), df['Proj'].unique()))
>>> df.sort_values(['Proj', 'Date'], inplace=True)
>>> df['CumCount'] = df.groupby(['Proj', 'Type']).cumcount() + 1
>>> df.drop_duplicates(['Date', 'Type', 'Proj'], keep='last', inplace=True)
>>> df = df.pivot_table(values='CumCount', index=['Date', 'Proj'], columns='Type')
>>> df.reindex(index).unstack('Proj').fillna(method='ffill').fillna(0).stack()
Type item1 item2
Date Proj
2013-01-01 PJ1 1.0 0.0
PJ2 0.0 0.0
2014-01-01 PJ1 1.0 1.0
PJ2 0.0 0.0
2015-01-01 PJ1 1.0 2.0
PJ2 0.0 0.0
2016-01-01 PJ1 2.0 2.0
PJ2 2.0 0.0
Credit To: stackoverflow.com
Related Query
- Grouped, binned, cumulative sum in Pandas
- Calculate cumulative sum forward pandas
- In pandas dataframe - returning last value of cumulative sum that satisfies condition
- Interrupt conditional cumulative sum pandas python
- Pandas Panel Data - Returns rolling cumulative sum with year gaps
- How to reset cumulative sum per group when a certain column is 0 in pandas
- Python Pandas - Cumulative sum by date (Month-Year) and unique user
- multiple cumulative sum based on grouped columns
- Using Numpy where to place conditions on Pandas DataFrame while taking Cumulative Sum
- How to get sum of values grouped by the pandas DataFrame and make numpy matrix?
- What's the best way to sum all values in a Pandas dataframe?
- Perform a reverse cumulative sum on a numpy array
- numpy histogram cumulative density does not sum to 1
- Running Cumulative sum of 1d NumPy Array
- Summing rows in grouped pandas dataframe and return NaN
- All row sum with pandas except one
- Using Pandas to Find Minimum Values of Grouped Rows
- How to get the cumulative sum of numpy array in-place
- Python Running cumulative sum with a given window
- Most efficient way to sum huge 2D NumPy array, grouped by ID column?
- Multidimensional cumulative sum in numpy
- Calculate cumulative sum from last non-zero entry in python
- sum up two pandas dataframes with different indexes element by element
- Sum ignoring strings in pandas dataframe
- How to create sum of columns in Pandas based on a conditional of multiple columns?
- Vectorize a 6 for loop cumulative sum in python
- Grouped By, Weighted, Column Averages in Pandas
- A fast numpy way to find index in array where cumulative sum becomes greater?
- Cumulative sum of variable till a given percentile
- How to accumulate unique sum of columns across pandas index
More Query from same tag
- How to get rid of numpy "bands" containing zeros
- How to compute matrix operation with TensorFlow?
- Efficient Way to create a 2d array of random shuffling of a range of numbers
- pandas Series' object has no attribute 'find'
- Create Numpy Array Representing a Geometric Shape
- mathplotlib six.moves.urllib.request import error
- Double dataframe values when certain condition is met using Python
- Numpy where and division by zero
- Difference between numpy.arr((...)) and numpy.arr([...])
- ploting two scattered graph in one graph
- Most Pythonic way to prevent adding numbers to an array slot, or cap value, if addition will take number outside its datatype range
- NumPy: Uniformly distributed N-dimensional samples
- PIL image to numpy
- Why does Panel.groupby() not accept level parameter in pandas 0.14?
- Tensorflow Create Protobuf for a Tensor