fillna

Filling na values with merge from another dataframe

扶醉桌前 提交于 2021-02-18 12:11:07
问题 I have a column with na values that I want to fill according to values from another data frame according to a key. I was wondering if there is any simple way to do so. Example: I have a data frame of objects and their colors like this: object color 0 chair black 1 ball yellow 2 door brown 3 ball **NaN** 4 chair white 5 chair **NaN** 6 ball grey I want to fill na values in the color column with default color from the following data frame: object default_color 0 chair brown 1 ball blue 2 door

Filling na values with merge from another dataframe

一曲冷凌霜 提交于 2021-02-18 12:10:29
问题 I have a column with na values that I want to fill according to values from another data frame according to a key. I was wondering if there is any simple way to do so. Example: I have a data frame of objects and their colors like this: object color 0 chair black 1 ball yellow 2 door brown 3 ball **NaN** 4 chair white 5 chair **NaN** 6 ball grey I want to fill na values in the color column with default color from the following data frame: object default_color 0 chair brown 1 ball blue 2 door

How do you fill NaN with mean of a subset of a group?

假如想象 提交于 2021-02-05 07:11:30
问题 I have a data frame with some values by year and type . I want to replace all NaN values in each year with the mean of values in that year with a specific type. I would like to do this in the most elegant way possible. I'm dealing with a lot of data so less computation would be good as well. Example: df =pd.DataFrame({'year':[1,1,1,2,2,2], 'type':[1,1,2,1,1,2], 'val':[np.nan,5,10,100,200,np.nan]}) I want ALL nan's regardless of their type to be replaced with their respective year mean of all

How do you fill NaN with mean of a subset of a group?

此生再无相见时 提交于 2021-02-05 07:08:18
问题 I have a data frame with some values by year and type . I want to replace all NaN values in each year with the mean of values in that year with a specific type. I would like to do this in the most elegant way possible. I'm dealing with a lot of data so less computation would be good as well. Example: df =pd.DataFrame({'year':[1,1,1,2,2,2], 'type':[1,1,2,1,1,2], 'val':[np.nan,5,10,100,200,np.nan]}) I want ALL nan's regardless of their type to be replaced with their respective year mean of all

How to pass another entire column as argument to pandas fillna()

99封情书 提交于 2021-01-29 14:53:19
问题 I would like to fill missing values in one column with values from another column, using fillna method. (I read that looping through each row would be very bad practice and that it would be better to do everything in one go but I could not find out how to do it with fillna .) Data before: Day Cat1 Cat2 1 cat mouse 2 dog elephant 3 cat giraf 4 NaN ant Data after: Day Cat1 Cat2 1 cat mouse 2 dog elephant 3 cat giraf 4 ant ant 回答1: You can provide this column to fillna (see docs), it will use

TypeError: only integer scalar arrays can be converted to a scalar index when use Pandas Fillna

喜夏-厌秋 提交于 2021-01-29 07:30:50
问题 Update : It seems due to .loc ,if i uses the original df from pd.read_excel, it is fine. I have a dataframe with Dtypes as follows. This is csv for the dataframe : CSV File Date datetime64[ns] Amout float64 Currency object ID object I used the following code to replace NaT, NaN a=np.datetime64('2000-01-01') values={'Date':a,'Amount':0,'Currency':'0','ID':'0'} df.fillna(value=values,inplace=True) However, I got the error : TypeError: only integer scalar arrays can be converted to a scalar