Francis Smart
Francis Smart

Reputation: 4065

Change How Pandas Displays nan

If I remember correctly, in Stata nan are displayed as .. Displaying them as such is particularly helpful if you have a lot of missing values as it draws emphasis to the filled values rather than the reverse. Is there any option in Pandas for an alternative nan display?

x = (pd.DataFrame(np.random.rand(10,5)) * 100).round()
x[x>10] = np.nan
x
    0       1       2       3       4
0   NaN     NaN     NaN     NaN     NaN
1   NaN     NaN     NaN     1.0     5.0
2   NaN     NaN     NaN     0.0     NaN
3   1.0     NaN     NaN     NaN     9.0
4   NaN     NaN     NaN     NaN     NaN

# Preferably a parameter that set this functionality:
x.fillna('.')
    0   1   2   3   4
0   .   .   .   .   .
1   .   .   .   1   5
2   .   .   .   0   .
3   1   .   .   .   9
4   .   .   .   .   .

Upvotes: 9

Views: 3272

Answers (1)

ev350
ev350

Reputation: 439

You can use Pandas styling, as shown here

df.style.set_na_rep(".")

Upvotes: 11

Related Questions