Cannon
Cannon

Reputation: 319

How can I fill my dataframe

Can someone please tell me how I can fill in the missing values of my dataframe? The missing values dont come up as NaN or anything that is common instead they show as two dots like .. How would i go about filling them in with the mean of that row that they are in?

                     1971     1990     1999     2000     2001     2002

Estonia                ..     17.4      8.3      8.5      8.5      8.6 
Spain                61.6    151.2    205.9    222.2    233.2    241.6 
SlovakRepublic       10.9     25.5     28.1     30.8     31.9     32.2   
Slovenia               ..     12.4     13.3     13.6     14.5     14.6  

My headers are the years and my index are the countries.

Upvotes: 3

Views: 54

Answers (2)

jezrael
jezrael

Reputation: 862581

It seems you can use mask, compare by numpy array created by values and replace by mean, last cast all columns to float:

print (df.mean(axis=1))
Estonia            10.26
Spain             210.82
SlovakRepublic     29.70
Slovenia           13.68

df = df.mask(df.values == '..', df.mean(axis=1), axis=0).astype(float)
print (df)
                 1971   1990   1999   2000   2001   2002
Estonia         10.26   17.4    8.3    8.5    8.5    8.6
Spain            61.6  151.2  205.9  222.2  233.2  241.6
SlovakRepublic   10.9   25.5   28.1   30.8   31.9   32.2
Slovenia        13.68   12.4   13.3   13.6   14.5   14.6

Upvotes: 2

Mike - SMT
Mike - SMT

Reputation: 15226

You should be able to use an .set_value

try df_name.set_value('index', 'column', value)

something like

df_name.set_value('Estonia','1971', 50)

Upvotes: 0

Related Questions