I have a Pandas dataframe, each row contains a name followed by many numbers in the columns. After a specific index for each row (calculated uniquely in every row), I want to set all the remaining values in that row to 0.
So, I tried out a few things and have the below working code:
for i in range(n):
index = np.where(df.columns == df['match_this_value'][i])[0].item()
df.iloc[i, index] = df['take_this_value'][i].day
df.iloc[i, (index+1):] = 0
However, this takes quite long as my dataset is very large. The runtime is about 70 seconds for my sample dataset, as my entire dataset is much longer. Is there a faster way to do this? Furthermore, is there a better way to do this manipulation without looping through each row?
EDIT: Sorry I should have specified how the index is calculated. the Index is calculated through an np.where by compared all of the columns of the dataframe (for each row) against one specific column and finding the match. so something like:
index = np.where(df.columns == df['match_this_value'][i])[0].item()
Once I have this index, I set the value at that column to the value of another column in the df. The entire code right now looks like this:
for i in range(n):
index = np.where(df.columns == df['match_this_value'][i])[0].item()
df.iloc[i, index] = df['take_this_value'][i].day
df.iloc[i, (index+1):] = 0