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?