I am trying to create a new column in an dataframe, by creating a dictionary based on an existing column and calling the 'map' function on the column. It seemed to be working for quite some time. However, the notebook started throwing
AttributeError: 'DataFrame' object has no attribute 'map'
I haven't changed the kernel or the python version. Here's the code i am using.
dict= {1:A,
2:B,
3:C,
4:D,
5:E}
# Creating an interval-type
data['new'] = data['old'].map(dict)
how to fix this?
Convert PySpark Dataframe to Pandas DataFramePySpark DataFrame provides a method toPandas() to convert it to Python Pandas DataFrame. toPandas() results in the collection of all records in the PySpark DataFrame to the driver program and should be done only on a small subset of the data.
Convert DataFrame Row to SeriesUse squeeze() function to convert the single Pandas DataFrame row to series. For instance, df. iloc[2]. reset_index(drop=True).
At times, you may need to convert your pandas dataframe to List. To accomplish this task, ' tolist() ' function can be used.
map is a method that you can call on a pandas.Series object. This method doesn't exist on pandas.DataFrame objects.
df['new'] = df['old'].map(d)
In your code ^^^ df['old'] is returning a pandas.Dataframe object for some reason.
Or perhaps your code isn't quite the same as the example you have given.
Either way the error is there because you are calling map() on a pandas.Dataframe object
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With