0

I have a Data frame in the following format:

df.columns=['Timestamp','Voltage','Temperature','string_1','string_2','string_3','string_4','string_5','string_6']

I want to run a for loop in the following manner:

for column in df[['string_1','string_2','string_3','string_4','string_5','string_6']]:

I want to run this for loop without mentioning the column names separately. I want to include all columns with the word "string" in it. How do I do this? A small sample of the data:

{'Timestamp': {0: '2019-01-01 06:00:00+00:00',
  1: '2019-01-01 06:05:00+00:00',
  2: '2019-01-01 06:10:00+00:00',
  3: '2019-01-01 06:15:00+00:00',
  4: '2019-01-01 06:20:00+00:00',
  5: '2019-01-01 06:25:00+00:00',
  6: '2019-01-01 06:30:00+00:00'},
 'Voltage': {0: 500, 1: 500, 2: 500, 3: 500, 4: 500, 5: 500, 6: 500},
 'Temperature': {0: 25, 1: 25, 2: 25, 3: 25, 4: 25, 5: 25, 6: 25},
 'string_1': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7},
 'string_2': {0: 3, 1: 4, 2: 1, 3: 2, 4: 5, 5: 8, 6: 9},
 'string_3': {0: 2, 1: 4, 2: 5, 3: 5, 4: 3, 5: 4, 6: 9},
 'string_4': {0: 1, 1: 4, 2: 7, 3: 2, 4: 2, 5: 3, 6: 1},
 'string_5': {0: 3, 1: 4, 2: 1, 3: 2, 4: 5, 5: 8, 6: 9},
 'string_6': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7}}
Ayush Sinha
  • 41
  • 1
  • 7

2 Answers2

1

Use DataFrame.filter:

df.filter(like='string')
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

Try pd.DataFrame.filter:

df.filter(like='string')
U13-Forward
  • 69,221
  • 14
  • 89
  • 114