This is a basic question, but I want to loop through a list of DataFrames and for each DataFrame, set the index as one of the columns in the DataFrame.
The issue with the code below is that it doesn't save the DataFrame with a new index. How do I format this For loop so that the DataFrames are permanently changed outside of the for loop? Thanks.
dflist = [df_1, df_2, df_3]
for df in dflist:
df = df.set_index('column_2')
As you have probably guessed, i is just a temporary value. Use i as only an index using enumerate
for i, df in enumerate(dflist):
dflist[i] = dflist[i].set_index('column_2'))
for i in dflist:
i.set_index('column_2', inplace=True)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With