Home > Software engineering >  how to iterate through multiple columns in pandas and change values?
how to iterate through multiple columns in pandas and change values?

Time:09-23

I have a data frame similar to the data frame below:

   column 1          column 2

4. Excellent         2. Poor
3. Good              2. Poor
2. Poor              1. No
1. No                4. Excellent

I am using pandas to analyse this data frame. I want to automatically change values in each row of a data frame, but I know how to do this column by column. Is it possible to iterate through all columns and change the values of all columns based on certain conditions in one go? In my case I want to change the strings "1. no" by integer 0; "2. Poor" by integer 2; "3. Good" by integer 3; and "4. Excellent" by integer 4. Does anyone know how I can do this? My desired output is:

   column 1          column 2

      4                 2
      3                 2
      2                 1
      1                 4

I tried to do this using iteritems() but it is not possible. Ex:

for c, d in LTs_support.iteritems():
    if d == '4. Excellent':
        d.replace('4. Excellent', '4')

CodePudding user response:

You can use str.extract on all columns and the ^(\d ) regex to catch the initial number:

df = df.apply(lambda c: c.str.extract('^(\d )', expand=False)).astype(int)

output:

  column 1 column 2
0        4        2
1        3        2
2        2        1
3        1        4

CodePudding user response:

Try this:

>>> df.apply(lambda x : x.str[:1])

  column 1 column 2
0        4        2
1        3        2
2        2        1
3        1        4
  • Related