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
2
votes
2 answers

pandas, how only fillna for last row with preceding line closest non-nan value

import pandas as pd import numpy as np df = pd.DataFrame(np.random.randint(0, 10, (10, 10)), columns=list('ABCDEFGHIJ')) df[df > 5] = np.nan for i in range(10): df.iloc[i, i % 10] = np.nan print(df) origin df is: A B C D E …
Zheng Xiaodong
  • 143
  • 3
  • 9
2
votes
1 answer

Replace NaN values in a Dataframe with average of same column with the same value from another column

I have a pandas dataframe that consists of multiple columns. For this question we only need to focus on the "Cholesterol" and "Age" column. In this dataframe each row represents a person and they all have an age. The Cholesterol column has a lot of…
2
votes
1 answer

How can I replace a nan value in an "object" column?

How can I replace a nan value in a column which is an 'object', contains other str and ensure it goes into the main df and not just the slice. I have tried this covid_19_df.Country_code.fillna('OT') but it is still empty D Country_code …
Nuro
  • 23
  • 5
2
votes
3 answers

Fill NAs : Min of values in group

Here is my DataFrame. df = pd.DataFrame ( {'CNN': ['iphone 11 63 GB TMO','iphone 11 128 GB ATT','iphone 11 other carrier','iphone 12 256 GB TMO','iphone 12 64 GB TMO','iphone 12 other carrier'], 'Family Name':['iphone…
Daman deep
  • 631
  • 3
  • 14
2
votes
2 answers

How to fill the gaps, and assigne the values, when using cumulative sum with Pandas?

I have following dataset (much larger, this is just small sample from it): City Year Votes Detroit 1964 23 Detroit 1977 61 Detroit 1978 89 Detroit 1986 116 Detroit 1993 144 Baltimore 1964 42 Baltimore 1965 91 Baltimore…
botafogo
  • 189
  • 7
2
votes
3 answers

R Fill NA with last value for a max of n times

There are multiple ways to fill missing values in R. However, I can't find a solution for filling just the last n NAs. Available options: na_vector <- c(1, NA, NA, NA, 2, 3, NA, NA) library(zoo) na.locf(na_vector) # Outputs: [1] 1 1 1 1 2 3 3…
2
votes
2 answers

Pandas fillna with custom lamda function - output not displayed correctly

I have two dataframes. The first one, grouper contains the mean sales quantity per month by item. Most items have values for all 12 months, because they have been sold for > 1 year. But the items that have been on sale < 1 year do not have values…
asuidncsdk
  • 67
  • 8
2
votes
7 answers

How to efficiently fill a column of a dataframe based on a dictionary

I have a dataframe and dictionary like this import pandas as pd import numpy as np df = pd.DataFrame({ 'A': [1, 1, 1, 2, 2, 3, 3, 3, 3], 'ignore_me': range(9), 'fill_me': [np.nan] * 9 }) di = { 1: ['a', 'b'], 2: ['c', 'd'], …
Cleb
  • 25,102
  • 20
  • 116
  • 151
2
votes
2 answers

Faster alternative to groupby, unstack then fillna

I'm currently doing the following operations based on a dataframe (A) made of two columns with multiple thousands of unique values each. >>> pd.DataFrame({ 'col1': ['foo', 'bar', 'bar', 'foo', 'baz', 'bar', 'baz'], 'col2': ['abc', 'def',…
Jivan
  • 21,522
  • 15
  • 80
  • 131
2
votes
1 answer

Pandas DataFrame conditional forward filling based on first row values

I have the following DataFrame: import pandas as pd df = pd.DataFrame({ 'col1':['A',pd.NA,pd.NA,pd.NA,pd.NA, 'B', pd.NA, pd.NA], 'col2':[9.5, 6,24,8, 30, 7, 6, 8], }) print(df) Giving: col1 col2 0 A 9.5 1 6.0 2
McBuch
  • 43
  • 6
2
votes
2 answers

Filling blanks in a pandas dataframe column leads to reversal of function

I have the following dataframe: a b x y language 0 id1 id_2 3 1 id2 id_4 6 ,0=/% 2 id3 id_6 9 |-|/# 3 id4 id_8 12 text4 I used langdetect to detect the language of the text elements in column y. This is the code I…
Tipo33
  • 181
  • 13
2
votes
1 answer

Pandas fillna() rows in a specific order

I have problem with fillna() method. This is my example df, which represents quantity of items in a shop. I would like to fill all the NaNs. If there is a NaN, I would like to fill it with values from previous day or if it is NaN, then from the next…
kstajer
  • 55
  • 4
2
votes
2 answers

Filling Missing Values Based on String Condition

I'm trying to write a function to impute some null values from a Numeric column based on string conditions from a Text column. My attempt example: def fill_nulls(string, val): if df['TextColumn'].str.contains(string) == True: df['NumericColumn']…
2
votes
1 answer

Fill nan value when condition meet

I have a dataframe as shown below: df = pd.DataFrame({'A': [1, np.nan, np.nan, np.nan, 3, 3, 3, 3, np.nan, np.nan, np.nan, 5, 5, 5, 6, 6, 6, np.nan, np.nan, np.nan, 6, 7, 8, 9, 10,np.nan, np.nan, 10, 11]}) I wanna fill the nan value only when the…
Ali
  • 113
  • 4
2
votes
3 answers

How to fill default values based on column names without typeerror - Pandas

I have a dataframe like as shown below obs = pd.DataFrame({'person_id' :[1,2,3],'obs_date':['12/31/2007','11/25/2009',np.nan], 'hero_id':[2,4,np.nan],'date':['12/31/2017',np.nan,'10/06/2015'], …
The Great
  • 7,215
  • 7
  • 40
  • 128