1

Is there a way to return the name/header of a column into a string in a pandas dataframe? I want to work with a row of data which has the same prefix. The dataframe header looks like this:

col_00 | col_01 | ... | col_51 | bc_00 | cd_00 | cd_01 | ... | cd_90

I'd like to apply a function to each row, but only from col_00 to col_51 and to cd_00 to cd_90 separately. To do this, I thought I'd collect the column names into a list, fe. to_work_with would be the list of columns starting with the prefix 'col', apply the function to df[to_work_with]. Then I'd change the to_work_with and it would contain the list of columns starting with the 'cd' prefix et cetera. But I don't know how to iterate through the column names.

So basically, the thing I'm looking for is this function:

to_work_with = column names in the df that start with "thisstring"

How can I do that? Thank you!

1
  • 2
    Do you want [col for col in df.columns.values if col.startswith("thisstring")]? Commented Jul 3, 2016 at 11:16

1 Answer 1

2

You can use boolean indexing with str.startswith:

cols = df.columns[df.columns.str.startswith('cd')]
print (cols)
Index(['cd_00', 'cd_01', 'cd_02', 'cd_90'], dtype='object')

Sample:

print (df)
   col_00  col_01  col_02  col_51  bc_00  cd_00  cd_01  cd_02  cd_90
0       1       2       3       4      5      6      7      8      9

cols = df.columns[df.columns.str.startswith('cd')]
print (cols)
Index(['cd_00', 'cd_01', 'cd_02', 'cd_90'], dtype='object')

#if want apply some function for filtered columns only
def f(x):
    return x + 1

df[cols] = df[cols].apply(f)    
print (df)
   col_00  col_01  col_02  col_51  bc_00  cd_00  cd_01  cd_02  cd_90
0       1       2       3       4      5      7      8      9     10

Another solution with list comprehension:

cols = [col for col in df.columns if col.startswith("cd")]
print (cols)
['cd_00', 'cd_01', 'cd_02', 'cd_90']
Sign up to request clarification or add additional context in comments.

5 Comments

Amazing! Thank you! :)
Glad can help you! Good luck!
Hi @jezrael very good one. I am able to create the Dataframe with values by just giving df[cols]. Can you please let me know the use of apply function there ? Sorry I am still in a learning process. So this might be a basic question to you.
@JKC - It is only a example for apply some function for columns filtered by startswith :)
No problem, I add ot to answer ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.