train_subset.iloc[:10, :10]
1 0 7045 17 16.0 0.0 0.0 0.0 0.0 18.0
1 0 8907 137 48.0 42.0 53.0 32.0 35.0 30.0
1 0 917 167 193.0 88.0 48.0 0.0 24.0 0.0
1 0 203 5303 2073.0 153.0 108.0 97.0 73.0 89.0
1 0 198 817 198.0 0.0 0.0 88.0 93.0 70.0
I have a dataset called train. I've printed out a small subset of it here.
Is there an efficient way to just REMOVE all zeroes from the dataset, updating the columns by shifting to the left? For example, my desired output would be...
Please note, I want this to only trigger starting on the 4th column. The first, second, and third column are meta-data and should not be changed.
train_subset.iloc[:10, :10]
1 0 7045 17 16.0 18.0
1 0 8907 137 48.0 42.0 53.0 32.0 35.0 30.0
1 0 917 167 193.0 88.0 48.0 24.0
1 0 203 5303 2073.0 153.0 108.0 97.0 73.0 89.0
1 0 198 817 198.0 88.0 93.0 70.0
Essentially, you can think of each row as a time-series, and I want to disregard any zeroes. So I only have readings at timepoints where events occur, and I've removed all zeroes between.
I can figure out how to remove zeroes and replace with NA's or whatever, but I cannot figure out how to forcibly shift each row to the left.