简体   繁体   中英

set new values in column based on condition

I have this DF

MARQUE  SAIS_COLL   TOURNEE ANNEE_COLL  SAISON  LIB_SAISON  DESIGNATION NUM_TAIL    TAILLE  PROFONDEUR  TAILLE_3    COL COLORIS ARTICLE EAN TARIF   DIVISION_COMMERCIALE    artkleurcode
0   ATG BA  0   2023    56B LA MUSE AFRICA  BANDEAU COQUE B T3  85  B   85B 13006   JA/JAUNE AFRICA EBB7156B    3597707754826   31.39   E9  EBB7156-13006
1   ATG BA  0   2023    56B LA MUSE AFRICA  BANDEAU COQUE B T4  90  B   90B 13006   JA/JAUNE AFRICA EBB7156B    3597707754833   31.39   E9  EBB7156-13006
2   ATG BA  0   2023    56B LA MUSE AFRICA  BANDEAU COQUE B T5  95  B   95B 13006   JA/JAUNE AFRICA EBB7156B    3597707754840   31.39   E9  EBB7156-13006
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
8   ATG BA  0   2023    56B LA MUSE AFRICA  BANDEAU COQUE D T3  85  D   85D 13006   JA/JAUNE AFRICA EBB7156D    3597707755021   32.89   E9  EBB7156-13006
9   ATG BA  0   2023    56B LA MUSE AFRICA  BANDEAU COQUE D T4  90  D   90D 13006   JA/JAUNE AFRICA EBB7156D    3597707755038   32.89   E9  EBB7156-13006
10  ATG BA  0   2023    56B LA MUSE AFRICA  BANDEAU COQUE D T5  95  D   95D 13006   JA/JAUNE AFRICA EBB7156D    3597707755045   32.89   E9  EBB7156-13006

I try to change only the values in column 'TAILLE' >60 by a new value that is old value-15. Problem is that the column has int and str values.

I tried

dct_of_sizes = {80: 65,
 85: 70,
 90: 75,
 95: 80,
 100: 85,
 105: 90,
 110: 95,
 115: 100,
 120: 105,
 125: 110,
 1: 1,
 2: 2,
 3: 3,
 4: 4,
 5: 5,
 6: 6,
 'XS': 'XS',
 'S': 'S',
 'M': 'M',
 'L': 'L',
 'XL': 'XL',
 'XXL': 'XXL',
 'T.U': 'ONE'}

df['TAILLE'] = df['TAILLE'].astype(str).map(dct_of_sizes)

But that doesn't work

TAILLE is 9th column

How to solve?

The first way:

import numpy as np

my_list = np.array(df['TAILLE'])
my_list_1 = []

for i in my_list:
  if type(i) is int:
    my_list_1.append(i-15)
  else:
    my_list_1.append(i)

df['TAILLE'] = my_list_1

The second way:

for index, row in df.iterrows():
  if type(df['TAILLE'][index]) is int:
    df['TAILLE'][index] = df['TAILLE'][index] - 15

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM