Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas update dataframe values if cell contains '-'

Tags:

python

pandas

I have a pandas Dataframe which has 46 columns, and 6 rows.

Index    Column1    Column2    Column3   Column4      ... # Cant type all 46 columns.
2012     5626       fooo       -         barrr
2013     5655h      booo       -         barr
2014     5626d      zooo       -         -
LTM      56         gooo       greed     -   

Is there a way for me to go through this Dataframe and update all the - values to be 0 or null values?

I have tried:

for zzz in df.columns:  # since df.columns will return me the names of the columns
    if df_final[zzz].any() == '-':
        df_final[zzz] = 0
        print(df_final) 

However, this just prints everything out as it is. it does not convert - into 0 / null

like image 596
jake wong Avatar asked Dec 06 '25 08:12

jake wong


1 Answers

use replace to replace that specific value with another one:

In [71]:
df.replace('-',0, inplace=True)
df

Out[71]:
  Index Column1 Column2 Column3 Column4
0  2012    5626    fooo       0   barrr
1  2013   5655h    booo       0    barr
2  2014   5626d    zooo       0       0
3   LTM      56    gooo   greed       0

Your code even if it would've worked is the wrong semantics:

for zzz in df.columns: 
    if df_final[zzz].any() == '-':
        df_final[zzz] = 0
        print(df_final) 

this: df_final[zzz] = 0 would've updated the entire column

if your code was:

for zzz in df.columns: 
    if df_final[zzz].any() == '-':
        df_final[zzz] = df_final[zzz].replace('-',0)
        print(df_final) 

then this would've only replace the rows that met the condition, you could've also done:

df.apply(lambda x: x.replace('-',0))

for a more compact method

EDIT if you want to replace with NaN then pass np.NaN instead of 0 above.

like image 131
EdChum Avatar answered Dec 07 '25 22:12

EdChum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!