I need to update a DataFrame column with some strings at selected rows, for which I have the index. So far, I managed to achieve what I need with list comprehension:
[data.particleIDs.values[idx[i]].append(particlenames[i]) for i in range(len(idx))]
where data.particleIDs
is the DataFrame column that needs to be updated, particlenames
a list containing the strings and idx
an array containing, for each string, the DataFrame row it needs to be written on. Several strings correspond to the same row, and I need to write them all in the DataFrame column.
Let's say I have a DataFrame and the list of strings that I use to update it:
data = pd.DataFrame({'particleIDs': [[] for i in range(20)]}
particlenames = ['c15001'+str(i) for i in range(10))]
I have 10 strings and I need to use them to update the rows [7 8 15 8 11 0 15 1 12 8]
in my DataFrame, i.e. I need to add each string to the corresponding row.
The FOR loop is terribly slow, as the actual particlenames
list is long and I need to repeat this process several times.
Is there anything I can do to speed this up?
Thank you!