How to write multiple values in a row in one command line with pandas

Viewed 198
df['column_a'][index] = 1
df['column_b'][index] = 2
df['column_c'][index] = 3

How to write 1,2 and 3 in one line? Would that consume 3x less time?

2 Answers

Use loc to do this in one assignment:

df.loc[index, ['column_a', ...]] = [1, 2, 3]

If index refers to a position and not a label, use

df.loc[df.index[index], ['column_a', ...]] = [1, 2, 3] 

Maybe this:

df['column_a'][index], df['column_b'][index], df['column_c'][index] = 1, 2, 3
Related