Replace Empty Values with Value from Other Column in a Dataframe

pandas replace empty cell with value of another column

You can replace '' by NA, then bfill:

df.replace('', pd.NA).bfill(axis=1)

Or use fillna:

df['A'] = df['A'].replace('', pd.NA).fillna(df['B'])

output:

    A   B
0 1 6
1 2 7
2 8 8
3 4 9
4 10 10

Replace the empty values in column based on another column Pandas

We could use GroupBy.apply with ffill and bfill:

df.groupby('Corp').apply(lambda x: x.ffill().bfill())

Corp TDS VVT SOAR
0 Steam 3429.0 450.0 NaN
1 Steam 3429.0 1365.0 NaN
2 Bliz 3425.1 34.0 11.0
3 Bliz 353.3 34.0 11.0
4 Tesla 2243.3 NaN 18.0
5 Tesla 2243.3 NaN 32.0
6 OYV NaN 15.0 16.0
7 OYV NaN 15.0 16.0

If you have blanks:

df.replace(r'^\s*$', np.nan, regex=True)\
.groupby('Corp').apply(lambda x: x.ffill().bfill())

Replace empty values with value from other column in a dataframe

You need to index also the replacing column:

df[ df$baglocatie == "", "baglocatie"  ]  <- df[ df$baglocatie == "", "knmilocatie" ]

Python Pandas replace NaN in one column with value from corresponding row of second column

Assuming your DataFrame is in df:

df.Temp_Rating.fillna(df.Farheit, inplace=True)
del df['Farheit']
df.columns = 'File heat Observations'.split()

First replace any NaN values with the corresponding value of df.Farheit. Delete the 'Farheit' column. Then rename the columns. Here's the resulting DataFrame:

resulting DataFrame

Replace blank value in dataframe based on another column condition

Hi I have used the below code and it worked

b = [52]
df.Item=np.where(df.Department.isin(b),df.Item.fillna(2515),df.Item)
a = [7]
df.Item=np.where(df.Department.isin(a),df.Item.fillna(45),df.Item)

Hope it helps someone who face the same issue

Replace empty value from colum by first word from another column's string

Not the best idea, but I believe it would solve your problem:

for index, row in df_opensports.iterrows():
if row["Sub_Categoria"] == None:
df_opensports.loc[index, "Sub_Categoria"] = row["Name"].split(" ")[0]

Thanks to your comment, I remembered another approach for this question:

df_opensports.Sub_Categoria.fillna(df_opensports.Name.apply(lambda x: x.split(" ")[0]), inplace=True)

Fill blank cells with another column value in Python

replace() the empty strings with nan and then chain a couple fillna():

df.C = df.C.replace(r'^\s*$', np.nan, regex=True).fillna(df.A).fillna(df.B)

# A B C
# 0 xyz NaN 12.03.2010
# 1 abc NaN 01.10.2009
# 2 NaN 14.11.2010 14.11.2010
# 3 02.10.2010 NaN 02.10.2010

Alternatively start with str.strip() to make the replacement simpler:

df.C = df.C.str.strip().replace('', np.nan).fillna(df.A).fillna(df.B)

Replace empty values based on part of the text from another variable in Pandas dataframe, using filter and regex expression

Try:

mask = frame.Mount.isna()
frame.loc[mask, "Mount"] = frame.loc[mask, "Lens"].str.extract(r"til\s+(.*)")[0]
print(frame)

Prins:

                                   Lens    Mount
0 Canon EF 50mm f/1.8 STM Canon E
1 Zeiss Planar T* 85mm f/1.4 til Canon Canon


Related Topics



Leave a reply



Submit