Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming the column headers from an iterative list of ranges using Lambda

opvalue_time.columns = opvalue_time.rename(lambda x: "Vol_" + x for x in range(4,80,1))

i already have a dataset with 80 columns , need to update the column headers as Vol_4, ....Vol_80. Currently the columns are 0(no headers)

like image 263
Prashant Avatar asked Jan 19 '26 12:01

Prashant


1 Answers

I don't think the rename method can directly process a generator expression. Instead, you can use a dictionary comprehension to create a mapping of old column names to new ones, or directly assign a new list of column names to opvalue_time.columns. You could try this instead:

opvalue_time.columns = ["Vol_" + str(x) for x in range(4, 80)]

This assumes your DataFrame initially has 76 columns, matching the range specified (you may need to adjust the range as necessary)

like image 157
Robert Long Avatar answered Jan 22 '26 00:01

Robert Long



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!