简体   繁体   中英

Filling None values in the dataframe in rows Pandas Python

Here is the data:

    o   h   l   c
Time                
2018-10-12 09:35:00 1.15868 1.15890 1.15868 1.15884
2018-10-12 09:36:00 1.15887 1.15889 1.15869 1.15869
2018-10-12 09:37:00 1.15869 1.15890 1.15869 1.15883
2018-10-12 09:38:00 1.15883 1.15894 1.15881 1.15888
2018-10-12 09:39:00 1.15888 1.15903 1.15887 1.15894

I am trying to replace the complete row with Nan values leaving only one. So the final output of the above dataframe must be something like this:

    o   h   l   c
Time                
nan nan nan nan
nan nan nan nan
nan nan nan nan
nan nan nan nan
2018-10-12 09:39:00 1.15888 1.15903 1.15887 1.15894

I tried the following and got error while attempting it:

df.fillna(inplace=True,value=None)

ValueError: must specify a fill method or value

I want to know how i can fill the nan values in place of the first four rows?
Kindly, let me know what I can do.

Use iloc :

df.iloc[:-1] = np.nan
print (df)
                           o        h        l        c
Time                                                   
2018-10-12 09:35:00      NaN      NaN      NaN      NaN
2018-10-12 09:36:00      NaN      NaN      NaN      NaN
2018-10-12 09:37:00      NaN      NaN      NaN      NaN
2018-10-12 09:38:00      NaN      NaN      NaN      NaN
2018-10-12 09:39:00  1.15888  1.15903  1.15887  1.15894

For missing values in index:

df = df.set_index(np.append([np.nan] * (len(df)-1), df.index[-1])).rename_axis(df.index.name)
print (df)
                           o        h        l        c
Time                                                   
NaT                      NaN      NaN      NaN      NaN
NaT                      NaN      NaN      NaN      NaN
NaT                      NaN      NaN      NaN      NaN
NaT                      NaN      NaN      NaN      NaN
2018-10-12 09:39:00  1.15888  1.15903  1.15887  1.15894

Another idea:

df1 = pd.DataFrame(index=df.index, columns=df.columns, data=df.iloc[[-1]])

print (df1)
                           o        h        l        c
Time                                                   
2018-10-12 09:35:00      NaN      NaN      NaN      NaN
2018-10-12 09:36:00      NaN      NaN      NaN      NaN
2018-10-12 09:37:00      NaN      NaN      NaN      NaN
2018-10-12 09:38:00      NaN      NaN      NaN      NaN
2018-10-12 09:39:00  1.15888  1.15903  1.15887  1.15894

df1 = pd.DataFrame(index=np.append([np.nan] * (len(df)-1), df.index[-1]), 
                   columns=df.columns, 
                   data=df.iloc[[-1]]).rename_axis(df.index.name)

print (df1)
                           o        h        l        c
Time                                                   
NaT                      NaN      NaN      NaN      NaN
NaT                      NaN      NaN      NaN      NaN
NaT                      NaN      NaN      NaN      NaN
NaT                      NaN      NaN      NaN      NaN
2018-10-12 09:39:00  1.15888  1.15903  1.15887  1.15894

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM