I am trying to build a function that will take a python data frame and check any column that is an object, then label encode and transform all the object that types and return a transformed data frame
I first load the data
data = pd.read_csv("data.csv")
then run
def preprocessing(data):
from sklearn.preprocessing import LabelEncoder
if data.dtypes=='object':
data = LabelEncoder().fit_transform(data)
return data
I call the variable below
final_data = preprocessing(data)
But i get the below error
ValueError Traceback (most recent call last)
/var/folders/ym/pwrd76_54wd1tj2h9kbvn1hh0000gn/T/ipykernel_76454/4286937000.py in ?()
----> 1 final_data = preprocessing(data)
/var/folders/ym/pwrd76_54wd1tj2h9kbvn1hh0000gn/T/ipykernel_76454/738215589.py in ?(data)
1 def preprocessing(data):
2 from sklearn.preprocessing import LabelEncoder
----> 3 if data.dtypes=='object':
4 data = LabelEncoder().fit_transform(data)
5 return data
~/anaconda3/envs/angaza/lib/python3.11/site-packages/pandas/core/generic.py in ?(self)
1525 @final
1526 def __nonzero__(self) -> NoReturn:
-> 1527 raise ValueError(
1528 f"The truth value of a {type(self).__name__} is ambiguous. "
1529 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
1530 )
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
3