Home > Net >  Assigning multiple values to the same string during dictionary mapping (pandas)
Assigning multiple values to the same string during dictionary mapping (pandas)

Time:11-10

I have the following code below.

I am trying to carry out a mapping where the value 0 in the dataframe column 'caffeine' is replaced by 'no' and any other value aside 0 is replaced by 'yes'.

However, the following command, the values that are not 0 are replaced with 'NaN' rather than 'yes'.

Would be so grateful for a helping hand!

newerdf = newdf.copy()
newerdf['caffeine'].max()
newerdf['caffeine'] = newerdf['caffeine'].map({0:'no',(1,2,3,4,5,6,7,8,9,10):'yes'})
newerdf.groupby(['caffeine'])['distance'].mean()
newdf['caffeine']

0      0.0
1      3.0
2      1.0
3      2.0
5      1.0
      ... 
911    1.0
912    1.0
913    2.0
914    1.0
915    2.0
newerdf['caffeine']:

0       no
1      NaN
2      NaN
3      NaN
5      NaN
      ... 
911    NaN
912    NaN
913    NaN
914    NaN
915    NaN

CodePudding user response:

here is one way to do it

# check value of caffeine, if it zero, then map the boolean result to Yes, No
df['caffeine']=df['caffeine'].eq(0).map({True:'no', False:'yes'})

CodePudding user response:

You can actually use np.where() to achieve the logic you are stating, without the need to state all potential values besides 0. It functions in a vectorized way similar to Excel's IF function.

newerdf['caffeine'] = np.where(newerdf['caffeine'] == 0,'no','yes')

CodePudding user response:

The way you define your dictionary doesn't make sense, it suggests you want to map the exact (1,2,3,4,5,6,7,8,9,10) tuple to 'yes'.

You would need to use a reverse mapping:

d = {'no': [0], 'yes': (1,2,3,4,5,6,7,8,9,10)}

d2 = {k:v for v,l in d.items() for k in l}

newerdf['caffeine'] = newerdf['acaffeine'].map(d2)

Output:

0       no
1      yes
2      yes
3      yes
5      yes
      ... 
911    yes
912    yes
913    yes
914    yes
915    yes

CodePudding user response:

Thank you all - all of the above solutions work :) I am so grateful.

  • Related