0

I am trying to replace "-" by 'NA' for each dataframe which is inside a python dictionary, for this task I am using a for-loop, however it runs without error and doesn't perform the desired task

Code:

import pandas as pd

df1= pd.DataFrame(data={'col1': [1, 2, "-"], 'col2': [3, "-", 4]})
df2 = pd.DataFrame(data={'col3':[1,2,3,"-",5], 'col4':[1,2,'NA', '-', 'NA'], 'col5':['John', 'Mary', 'Gordon', 'Cynthia', 'Marianne']})
df3 = pd.DataFrame(data={'col6':[19, "-",20, 23]})

## dataframes dictionary ##

all_dataframes = {"df1": df1, "df2": df2, "df3": df3}

############################## removing "-"  #####################################

for k, v in all_dataframes.items():
    if v is '-':
        all_dataframes[k] = 'NA'
        

Output:

#{'df1':   col1 col2
# 0    1    3
# 1    2    -
# 2    -    4,
# 'df2':   col3 col4      col5
# 0    1    1      John
# 1    2    2      Mary
# 2    3   NA    Gordon
# 3    -    -   Cynthia
# 4    5   NA  Marianne,
# 'df3':   col6
# 0   19
# 1    -
# 2   20
# 3   23}

Expected Output:

#{'df1':   col1 col2
# 0    1    3
# 1    2    NA
# 2    NA    4,
# 'df2':   col3 col4      col5
# 0    1    1      John
# 1    2    2      Mary
# 2    3   NA    Gordon
# 3    NA    NA   Cynthia
# 4    5   NA  Marianne,
# 'df3':   col6
# 0   19
# 1    NA
# 2   20
# 3   23}

What could I adjust in the for-loop to accomplish the task?

1 Answer 1

1

It cannot work since v is a Dataframe, not an element of it. Supposing, you want to keep the procedure from above, try:

for k, v in all_dataframes.items():
     v.replace({'-': 'NaN'}, regex=True, inplace=True)
     #print(v)

Returns:

{'df1':   col1 col2
 0    1    3
 1    2  NaN
 2  NaN    4,
 'df2':   col3 col4      col5
 0    1    1      John
 1    2    2      Mary
 2    3   NA    Gordon
 3  NaN  NaN   Cynthia
 4    5   NA  Marianne,
 'df3':   col6
 0   19
 1  NaN
 2   20
 3   23}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.