Questions tagged [fillna]

Use this tag for pandas.DataFrame.fillna or pandas.Series.fillna

Fill missing values using the specified method for a DataFrame or a Series with pandas (see also interpolate, ffill, bfill)

Docs:

528 questions
5
votes
2 answers

Pandas - fillna with mean for specific categories

I'd like to fillna with the mean number for the column but only for representatives of the same category as the missing value data = {'Class': ['Superlight', 'Aero', 'Aero', 'Superlight', 'Superlight', 'Superlight', 'Aero', 'Aero'], …
Wasteland
  • 4,889
  • 14
  • 45
  • 91
5
votes
2 answers

Using fillna with two multi-index dataframes throws InvalidIndexError

I have two dataframes like this: import pandas as pd import numpy as np df1 = pd.DataFrame({ 'key1': list('ABAACCA'), 'key2': list('1675987'), 'prop1': list('xyzuynb'), 'prop2': list('mnbbbas') }).set_index(['key1', 'key2']) df2 =…
Cleb
  • 25,102
  • 20
  • 116
  • 151
5
votes
3 answers

fillna not replacing nan values in the dataframe

I am trying to replace the nan values in a dataframe column 'Functional' using fillna() function. The issues I am facing are below: I am able to detect the null values using the isnull() dfcomp[dfcomp['Functional'].isnull()==True] search for null…
PVL
  • 51
  • 1
  • 1
  • 4
4
votes
1 answer

Pandas: Fill na with mode of a group

I have a df with multiple columns. df = pd.DataFrame({'Store':['M1','M2','M3','M1','M1','M2','M2','M3','M3'], 'Category':['A','A','A','B','B','B','C','C','C'], …
4
votes
1 answer

pandas fillna by group for multiple columns

In a dataset like this one (CSV format), where there are several columns with values, how can I use fillna alongside df.groupby("DateSent") to fill in all desired columns with min()/3 of the group? In [5]: df.head() Out[5]: ID DateAcquired …
paropunam
  • 488
  • 2
  • 11
4
votes
1 answer

How to fill NANs "ignoring" the index?

I have two dataframes like this: import pandas as pd import numpy as np df1 = pd.DataFrame( { 'A': list('abdcde'), 'B': ['s', np.nan, 'h', 'j', np.nan, 'g'] } ) df2 = pd.DataFrame( { 'mapcol': list('abpppozl') …
Cleb
  • 25,102
  • 20
  • 116
  • 151
4
votes
1 answer

Fillna with backwards and forward looking condition in Pandas

I am working with a dataframe that has a column with several NaN that I want to fill according to the following condition: If going backwards and forward up to 3 rows there are 2 equal values, then fill the NaN with that value. Since this might not…
4
votes
1 answer

Fill Na in multiple columns with values from another column within the pandas data frame

Pandas version 0.23.4, python version 3.7.1 I have a dataframe df as below df = pd.DataFrame([[0.1, 2, 55, 0,np.nan], [0.2, 4, np.nan, 1,99], [0.3, np.nan, 22, 5,88], [0.4, np.nan, np.nan,…
Shijith
  • 4,602
  • 2
  • 20
  • 34
4
votes
3 answers

Pandas fillna() not working on DataFrame slices

Pandas fillna is not working on DataFrame slices, here is an example df = pd.DataFrame([[np.nan, 2, np.nan, 0], [3, 4, np.nan, 1], [np.nan, np.nan, np.nan, 5], [np.nan, 3, np.nan, 4]], …
HP6
  • 53
  • 1
  • 6
4
votes
1 answer

How to apply different method parameters for different columns in pandas fillna() method

I have a data frame like this. Name Roll GPA A 10 5.0 B NaN 4.5 C 12 NaN I am using: df.fillna(method='ffill', inplace=True) But it fills all the columns using ffill method. But I want to fill the NaN value…
Adnan Toky
  • 1,756
  • 2
  • 11
  • 19
4
votes
1 answer

Pandas fillna with an incremented value

I have a dataframe with a column of sequential but not adjacent numbers and missing values. I'd like to use the fillna function to fill in the missing values with an incremented value from the previous non-missing row. Here's a simplified…
Eric M
  • 109
  • 1
  • 6
4
votes
4 answers

Fill Consecutive NaNs in Pandas Series

I want to fill missing values in my pandas series, if there are less than 3 consecutive NANs. Original series with missing values: s=pd.Series(pd.np.random.randn(20)) s[[1,3,5,7,12,13,14,15, 18]]=pd.np.nan Gives: 0 0.444025 1 NaN 2 …
EHB
  • 1,127
  • 3
  • 13
  • 24
4
votes
2 answers

Python, how to fill nulls in a data frame using a dictionary

I have a dataframe df something like this A B C 1 'x' 15.0 2 'y' NA 3 'z' 25.0 and a dictionary dc something like dc = {'x':15,'y':35,'z':25} I want to fill all nulls in column C of the dataframe using values of…
Manoj Agrawal
  • 775
  • 3
  • 8
  • 20
4
votes
2 answers

pandas replace only part of a column

Here is my input: import pandas as pd import numpy as np list1 = [10,79,6,38,4,557,12,220,46,22,45,22] list2 = [4,3,23,6,234,47,312,2,426,42,435,23] df = pd.DataFrame({'A' : list1, 'B' : list2}, columns = ['A', 'B']) df['C'] = np.where (df['A'] >…
bud fox
  • 335
  • 3
  • 16
3
votes
4 answers

fillna by avoiding row wise operation in pandas

I have a data frame in which there is a column containing several NaN values. The dataframe looks like this: col_1 col_2 2022-10-31 99.094 102.498 2022-11-30 99.001 101.880 2022-12-31 NaN 108.498 2023-01-31 NaN 100.500 I…
EMT
  • 458
  • 3
  • 14
1
2
3
35 36