Pandas - Processing NaN in Categorical Data

I have a column in the data area that has categorical data, but some data is missing, i.e. NaN. I want to do linear interpolation from this data to fill in the missing values, but I'm not sure how to do it. I cannot refuse NaN to turn the data into a categorical type, because I need to fill them. A simple example demonstrating what I'm trying to do.

col1  col2
5     cloudy
3     windy
6     NaN
7     rainy
10    NaN

Say I want to convert col2to categorical data, but save the NaNs and populate them using linear interpolation, how do I do this. Suppose that after converting a column to categorical data, it looks like this:

col2
1
2
NaN
3
NaN

Then I can do linear interpolation and get something like this

col2
1
2
3
3
2

How can i achieve this?

+5
2

UPDATE:

, .. 1,2 3 , ?

: DF:

In [129]: df
Out[129]:
   col1    col2
0     5  cloudy
1     3   windy
2     6     NaN
3     7   rainy
4    10     NaN
5     5  cloudy
6    10     NaN
7     7   rainy

In [130]: df.dtypes
Out[130]:
col1       int64
col2    category
dtype: object

In [131]: df.col2 = (df.col2.cat.codes.replace(-1, np.nan)
     ...:              .interpolate().astype(int).astype('category')
     ...:              .cat.rename_categories(df.col2.cat.categories))
     ...:

In [132]: df
Out[132]:
   col1    col2
0     5  cloudy
1     3   windy
2     6   rainy
3     7   rainy
4    10  cloudy
5     5  cloudy
6    10  cloudy
7     7   rainy

OLD "" :

IIUC :

In [66]: df
Out[66]:
   col1    col2
0     5  cloudy
1     3   windy
2     6     NaN
3     7   rainy
4    10     NaN

factorize col2:

In [67]: df.col2 = pd.factorize(df.col2, na_sentinel=-2)[0] + 1

In [68]: df
Out[68]:
   col1  col2
0     5     1
1     3     2
2     6    -1
3     7     3
4    10    -1

( -1 NaN):

In [69]: df.col2.replace(-1, np.nan).interpolate().astype(int)
Out[69]:
0    1
1    2
2    2
3    3
4    3
Name: col2, dtype: int32

, category dtype:

In [70]: df.col2.replace(-1, np.nan).interpolate().astype(int).astype('category')
Out[70]:
0    1
1    2
2    2
3    3
4    3
Name: col2, dtype: category
Categories (3, int64): [1, 2, 3]
+5

, , , . , .

"pad", :

df.interpolate(method='pad')

. ( )

0

Source: https://habr.com/ru/post/1667918/


All Articles