1

I am importing a csv that looks like this. The possible values are $ and net

Type1,Type2,Type3 $,, $,net, net,$,$

I currently have it in a pandas dataframe and am wondering how I can map $ and net to different values.

I want to map $ -> DEPOSIT and net to -> NET DEP

I am passing the column into a function

def directDeposit(var):
    for value in var: 
        var.astype(str)
        if value == '$':
            var = value.map({'$':'DEPOSIT'})

df['type1'] = directDeposit(df['type1'])`

Because there are three possible types from the csv I wanted to be able to pass the type I was testing into the function so that I could repeat it for all 3. I'm not really sure where to go from here.

glotchimo
  • 664
  • 5
  • 23
Logan0015
  • 177
  • 1
  • 9

2 Answers2

1

You can just do this:

df['type1'] = df['type1'].map({'net': 'NET DEP', '$': 'DEPOSIT', '': "No value assigned"})

When you create a function that takes a Pandas Series as an input, you don't have to loop over it. Pandas will do that for you.

1

You can use the replace method:

df.replace({'$': 'DEPOSIT', 'net': 'NET DEP'})
maxi.marufo
  • 411
  • 2
  • 11