2

Does anyone know how to concatenate a list of strings using a for loop?

This works, without a loop:

Extract Column Names

column_names = file.columns.tolist()
column_names[1]= column_names[1] + '_Vol_Adj'
column_names[1]

But does not work as part of the following loop:

for i in column_names[1:-2]:
    column_names[i]= column_names[i] + '_Vol_Adj'
    i = i+1
TypeError                                 Traceback (most recent call last)
<ipython-input-242-5509cede9f32> in <module>()
      2 
      3 for i in column_names[1:-2]:
----> 4     column_names[i]= column_names[i] + '_Vol_Adj'
      5     i = i+1

TypeError: list indices must be integers or slices, not str
jpp
  • 159,742
  • 34
  • 281
  • 339
arkadiy
  • 746
  • 1
  • 10
  • 26
  • 5
    `for i in column_names[1:-2]` does not iterate over the indices in the list. Try `print(i)` inside your loop and you'll see the issue. Related: [Accessing the index in 'for' loops?](https://stackoverflow.com/questions/522563/accessing-the-index-in-for-loops) – pault Jul 23 '18 at 21:53

3 Answers3

2

Your problem is that when you iterate over a the list column_names using for i in column_names[1:-2], the value of i will be the element in the list (not the corresponding index).

In your case, one simple thing would be to use enumerate, as shown in the following example:

column_names = ["a", "b", "c", "d", "e"]
for i, val in enumerate(column_names[1:-2]):
    column_names[i+1] += '_Vol_Adj'
print(column_names)
#['a', 'b_Vol_Adj', 'c_Vol_Adj', 'd', 'e']

I am updating the value at index i+1 inside the loop because we are starting the iteration at index 1.

Or you could use range(1, len(column_names)-2) as @Barmar suggested:

for i in range(1, len(column_names)-2):
    column_names[i+1] += '_Vol_Adj'
pault
  • 41,343
  • 15
  • 107
  • 149
2

Can also use add_suffix and df.rename

cols=['b', 'c']
ncols = df[cols].add_suffix('_suffix').columns

df.rename(columns={old:new for old,new in zip(cols,ncols)})
rafaelc
  • 57,686
  • 15
  • 58
  • 82
1

See @pault's solution for how you can adapt your for loop with enumerate or range.

Given you are using Pandas, you may be interested in a NumPy-based solution:

import pandas as pd, numpy as np

df = pd.DataFrame(columns=list('ABCDE'))

arr = df.columns.values
arr[1:-1] += '_Vol_Adj'
df.columns = arr

print(df.columns)

Index(['A', 'B_Vol_Adj', 'C_Vol_Adj', 'D_Vol_Adj', 'E'], dtype='object')

It's important that you do not modify df.columns.values directly, as this has side-effects. Here we've modified a copy of the underlying NumPy array and then assigned back to the dataframe.

jpp
  • 159,742
  • 34
  • 281
  • 339