'Manipulate Pandas dataframe based on index and column values
I have the following pandas dataframe (in code):
original_df = pd.DataFrame([['ID1', 4], ['ID1', 4], ['ID1', 4], ['ID2', 5], ['ID2', 5], ['ID2', 5], ['ID3', 6], ['ID3', 6], ['ID3', 6]], columns=['Index', 'Value'])
Based on each occurence of the change in Index value, only the first value column's integer should be maintained, and the rest of the values should be changed to 0 value.
The resulting manipulated dataframe should look like this:
desired_df = pd.DataFrame([['ID1', 4], ['ID1', 0], ['ID1', 0], ['ID2', 5], ['ID2', 0], ['ID2', 0], ['ID3', 6], ['ID3', 0], ['ID3', 0]], columns=['Index', 'Value'])
I have tried numerous different manipulation techniques, but none has worked. I have tried substitution of values, but this doesn't scale well for many rows at once. Please could someone provide any suggestions ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
