Edward Edward - 1 year ago 65
Python Question

Dict in loop for pd.DataFrame

I have many columns in my dataset & i need to change values in some of the variables. I do as below

import pandas as pd
import numpy as np
df = pd.DataFrame({'one':['a' , 'b']*5, 'two':['c' , 'd']*5, 'three':['a' , 'd']*5})


df1 = df[['one', 'two']]


map = { 'a' : 'd', 'b' : 'c', 'c' : 'b', 'd' : 'a'}

and loop

for i in df1.values:
np = [ map[x] for x in i]

then i change columns

df['one'] = [row[0] for row in df2]
df['two'] = [row[1] for row in df2]

It works but it's very long way. How to make it shorter?

Answer Source

You can use Series.map() iterating over columns:

cols = ['one', 'two']
mapd = { 'a' : 'd', 'b' : 'c', 'c' : 'b', 'd' : 'a'}

for col in cols:
    df[col] = df[col].map(mapd).fillna(df[col])

  one three two
0   d     a   b
1   c     d   a
2   d     a   b
3   c     d   a
4   d     a   b
5   c     d   a
6   d     a   b
7   c     d   a
8   d     a   b
9   c     d   a