简体   繁体   中英

How to add zero values in beginning and end of Pandas?

I have the following dataframe

    Inbound Value
1   Nan
2   Nan
3   Nan
4   ...
5   ...
19  Nan
20  130
21  130
22  140
23  140
24  170
25  170
25  170
26  ...
27  210
28  Nan
29  Nan
30  ...
..  ...
131 Nan

I would like to drop most of values which are Nan but keeping only 11 first values and keep also the last 11 Nan .

I know that data = data.dropna() drop all Nan values but I want to have as I described.

Use numpy's r_ to set range of indexes to a value and then drop remaining NaN

df.iloc[pd.np.r_[0:10, -11:0], df.columns.get_loc('Inbound Value')] = 0
data.dropna()

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