I have a dataframe that contains NA's, I need to fill the NA's with some value, however I need the same value per column, but every column must be different. I cannot use a dictionary for column to NA value mapping as I don't know how many columns due to the data being imported from an API.
I am using python 3
sample of DF and expected output:
DF:
Expected output:
CodePudding user response:
You can craft a dictionary with replacement values for each column containing NAs (here I'm using ascii uppercase letters, but you can use whatever source):
s = df.isna().any()
d = dict(zip(s[s].index, ascii_uppercase))
# {'A': 'A', 'B': 'B', 'D': 'C', 'E': 'D'}
df = df.fillna(d)
output:
a b c d e
0 - - - - D
1 A B - - -
2 A - - C D
used input:
a b c d e
0 - - - - NaN
1 NaN NaN - - -
2 NaN - - NaN NaN
alternative method to craft the dictionary
s = df.isna().any()
d = {k: v*3 '|' for k,v in zip(s[s].index, ascii_uppercase)}
# {'a': 'AAA|', 'b': 'BBB|', 'd': 'CCC|', 'e': 'DDD|'}
df = df.fillna(d)
output:
a b c d e
0 - - - - DDD|
1 AAA| BBB| - - -
2 AAA| - - CCC| DDD|
CodePudding user response:
IIUC:
maps = {col: i for i, col in enumerate(df.columns)}
df = df.fillna(maps)