0

I am currently in the phase of data preparation and have a certain issue I would like to make easy.

The content of my columns: 10 MW / color. All the columns which have this content are named with line nr. [int] or a [str]

What I want to display and which is the data of interest is the color. What I did was following: df['columnname'] = df['columnname'].str.split('/').str[-1]

The problem which occurs is that this operation should be done on all columns which names start with the string "line". How could I do this?

I thought about doing it with a for-loop or a while-loop but I am quite unsure how to do the referencing in the data frame then since I am a nooby in python for now.

Thanks for your help!

inneb
  • 1,060
  • 1
  • 9
  • 20
  • Please post sample data – Andrew L May 23 '17 at 11:51
  • Line Nr. 203 Line Nr. 205 17 MW / green 9 MW / green 2 MW / green 21 MW / green 5 MW / green 20 MW / green 16 MW / green 22 MW / green These are 2 columns but I have much more. So what I want to that the split operations runs through all those columns. Any suggestions? – inneb May 23 '17 at 12:13
  • I'm sorry I don't understand... Please read this as a guide to asking reproducible questions and update your answer: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – Andrew L May 23 '17 at 12:28

1 Answers1

0

Ok just solved the question:

with df.shape I found out what the dimensions are and then started a for loop: for i in range(1,x): df[df.columns[i]]= df[df.columns[i]].str.split('/').[-1]

If you have any more efficient ways let me know :)

inneb
  • 1,060
  • 1
  • 9
  • 20