i wanna change all the cells inside my data frame that are string and value = ''.
I have one data set that has 7 columns . for example:
a,b,c,d,e,f,g.
and has 700 rows. i wanna change the value of the cells in specific 5 columns in one code. I tried this:
columns = [a,b,c,d,e]
def get_tmp(i):
if len(i) == 0:
b ='tmp'
return b
else:
return i
weights_df[colun] = weights_df[colun].apply(get_tmp)
but this don't function.
to fix the problem i used a looping for:
columns = [a,b,c,d,e]
def get_tmp(i):
if len(i) == 0:
b ='tmp'
return b
else:
return i
for colun in columns:
weights_df[colun] = weights_df[colun].apply(get_tmp)
have another way to fix this situation using only .apply? if have, Do i need change somethin in my function ? what i need change ?
thank you guys.
CodePudding user response:
You can try this code.
import pandas as pd
import numpy as np
filename = 'Book1.xlsx'
weights_df= pd.read_excel(filename)
columns = ['a','b','c','d','e']
for col in columns:
weights_df[col] =
weights_df[col].apply(lambda x: 'tmp' if x=='' else x)
This code is working in my local.
CodePudding user response:
thank you Bright Gene, it's a good answer. in reality i wanna check if is possible to do the same changes with out the looping for.
I will be more clear, maybe i did some miscommunication.
this data frame have this columns a,b,c,d,e,f,g.
i wanna change only a party of these columns: a,b,c,d,e.
the other are numbers.
I created a list: columns_to_modify= [a,b,c,d,e]
I wanna try to change inside like this:
weights_df[columns_to_modify] = weights_df[columns_to_modify].apply(lambda x: 'tmp' if x=='' else x)
at this moment i wanna understand if have a way to apply only to a specific columns whit out lopping for.