You can't map a dataframe, but you can convert the dataframe to an RDD and map that by doing spark_df.rdd.map(). Prior to Spark 2.0, spark_df.map would alias to spark_df.rdd.map(). With Spark 2.0, you must explicitly call .rdd first.

Answer from David on Stack Overflow
Top answer
1 of 3
10

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.

  • As @jezrael points out this could be due to having more than one old column in the dataframe.
  • 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

2 of 3
4

Main problem is after selecting old column get DataFrame instead Series, so map implemented yet to Series failed.

Here should be duplicated column old, so if select one column it return all columns old in DataFrame:

df = pd.DataFrame([[1,3,8],[4,5,3]], columns=['old','old','col'])
print (df)
   old  old  col
0    1    3    8
1    4    5    3

print(df['old'])
   old  old
0    1    3
1    4    5

#dont use dict like variable, because python reserved word
df['new'] = df['old'].map(d)
print (df)

AttributeError: 'DataFrame' object has no attribute 'map'

Possible solution for deduplicated this columns:

s = df.columns.to_series()
new = s.groupby(s).cumcount().astype(str).radd('_').replace('_0','')
df.columns += new
print (df)
   old  old_1  col
0    1      3    8
1    4      5    3

Another problem should be MultiIndex in column, test it by:

mux = pd.MultiIndex.from_arrays([['old','old','col'],['a','b','c']])
df = pd.DataFrame([[1,3,8],[4,5,3]], columns=mux)
print (df)
  old    col
    a  b   c
0   1  3   8
1   4  5   3

print (df.columns)
MultiIndex(levels=[['col', 'old'], ['a', 'b', 'c']],
           codes=[[1, 1, 0], [0, 1, 2]])

And solution is flatten MultiIndex:

#python 3.6+
df.columns = [f'{a}_{b}' for a, b in df.columns]
#puthon bellow
#df.columns = ['{}_{}'.format(a,b) for a, b in df.columns]
print (df)
   old_a  old_b  col_c
0      1      3      8
1      4      5      3

Another solution is map by MultiIndex with tuple and assign to new tuple:

df[('new', 'd')] = df[('old', 'a')].map(d)
print (df)
  old    col new
    a  b   c   d
0   1  3   8   A
1   4  5   3   D

print (df.columns)
MultiIndex(levels=[['col', 'old', 'new'], ['a', 'b', 'c', 'd']],
           codes=[[1, 1, 0, 2], [0, 1, 2, 3]])
Discussions

Error when running ``make_mean_sem_table`` (AttributeError: 'DataFrame' object has no attribute 'map')
Describe the bug Running mlresearch.latex.make_mean_sem_table with a mean and sem dataframe results into an AttributeError. Steps/Code to Reproduce import numpy as np import pandas as pd from mlres... More on github.com
🌐 github.com
1
November 23, 2023
AttributeError: 'DataFrame' object has no attribute 'map' in version 2.7.0
What Happens? When I update my morph_kgc version from 2.6.4 to 2.7.0, without modifying my code, I now get an AttributeError To Reproduce config_ini = f"""[CONFIGURATION] output_form... More on github.com
🌐 github.com
2
April 2, 2024
Mapping multiple columns from one pandas data frame to another
I'm not a Pandas guy and, in fact, find it quite complex and uninituitive, so take what follows with a grain of salt. The problem you have is that DataFrames don't have a .map method but Series objects do. So, what's happening in your code to throw the error? The difference, I think, is that score['mop_'] (note the single brackets) returns a Series object, whereas score[['merchant_merchantid_','mop_']] (note the nested brackets) returns a DataFrame object which lacks a .map method. What can you do that might work? You could look at DataFrame.apply or DataFrame.applymap to see if they fit your use-case. Hope that helps! More on reddit.com
🌐 r/learnpython
3
3
January 13, 2023
AttributeError: 'DataFrame' object has no attribute 'map' when running qgrid.enable()
Environment Operating System: Windows 10 Python Version: `$3.8.5 How did you install Qgrid: pip Python packages: `$ qgrid==1.3.1, jupyter notebook 6.1.4 Description of Issue Tried to enable qgrid b... More on github.com
🌐 github.com
0
March 22, 2021
🌐
Spark By {Examples}
sparkbyexamples.com › home › hbase › attributeerror: ‘dataframe’ object has no attribute ‘map’ in pyspark
AttributeError: 'DataFrame' object has no attribute 'map' in PySpark - Spark By {Examples}
March 27, 2024 - PySpark DataFrame doesn’t have a map() transformation instead it’s present in RDD hence you are getting the error AttributeError: ‘DataFrame’ object has no attribute ‘map’
🌐
GitHub
github.com › joaopfonseca › ml-research › issues › 61
Error when running ``make_mean_sem_table`` (AttributeError: 'DataFrame' object has no attribute 'map') · Issue #61 · joaopfonseca/ml-research
November 23, 2023 - Describe the bug Running mlresearch.latex.make_mean_sem_table with a mean and sem dataframe results into an AttributeError. Steps/Code to Reproduce import numpy as np import pandas as pd from mlresearch.latex import make_mean_sem_table m...
Author   joaopfonseca
🌐
GitHub
github.com › morph-kgc › morph-kgc › issues › 229
AttributeError: 'DataFrame' object has no attribute 'map' in version 2.7.0 · Issue #229 · morph-kgc/morph-kgc
April 2, 2024 - What Happens? When I update my morph_kgc version from 2.6.4 to 2.7.0, without modifying my code, I now get an AttributeError To Reproduce config_ini = f"""[CONFIGURATION] output_format=N-QUADS [DataSource1] mappings=/tmp/{data_type}_rml....
Author   Stiksels
🌐
Brainly
brainly.com › computers and technology › high school › when will this error occur: `attributeerror: 'dataframe' object has no attribute 'map'?
[FREE] When will this error occur: `AttributeError: 'DataFrame' object has no attribute 'map'? - brainly.com
This **error **occurs when trying to use the map() function on a DataFrame object in Python's pandas library. The map() function is not supported directly on a DataFrame, but you can use the apply() **function **instead.
🌐
Brainly
brainly.com › computers and technology › high school › how can i solve this error: 'dataframe' object has no attribute 'map'?
[FREE] How can I solve this error: 'dataframe' object has no attribute 'map'? - brainly.com
November 19, 2023 - To solve the error 'dataframe' object has no attribute 'map', you need to use the correct method for mapping values in a Pandas DataFrame. The 'map' method is used to map values of a Series object to other values based on a provided mapping ...
🌐
Reddit
reddit.com › r/learnpython › mapping multiple columns from one pandas data frame to another
r/learnpython on Reddit: Mapping multiple columns from one pandas data frame to another
January 13, 2023 -

Mapping from one column to another such as below works fine, however the requirements have changed and now need to map two columns to the summary table, and am getting the error 'DataFrame' object has no attribute 'map'. I'm sure it is something simple like a bracket or parentheses out of place, but right now not quite sure.

score['#_%_to_Total'] = (score['Total_#_Genuine'] / score['mop_'].map(summary.set_index(['mop_'])['count_Not Fraud']))*100

#Below is the line of code giving the AttributeError
score['#_%_to_Total'] = (score['Total_#_Genuine'] / score[['merchant_merchantid_','mop_']].map(summary.set_index(['merchant_merchantid_','mop_'])['count_Not Fraud']))*100
AttributeError: 'DataFrame' object has no attribute 'map'
Find elsewhere
🌐
GitHub
github.com › quantopian › qgrid › issues › 360
AttributeError: 'DataFrame' object has no attribute 'map' when running qgrid.enable() · Issue #360 · quantopian/qgrid
March 22, 2021 - AttributeError: 'DataFrame' object has no attribute 'map' when running qgrid.enable()#360 · Copy link · info-rchitect · opened · on Mar 22, 2021 · Issue body actions · Operating System: Windows 10 · Python Version: `$3.8.5 · How did you install Qgrid: pip ·
Author   info-rchitect
🌐
PyTorch Forums
discuss.pytorch.org › data
Dataset object has no attribute map - data - PyTorch Forums
May 3, 2022 - Hello, I create a dataset object with the purpose to use map. However, when I try to call the function map, I get the error that the object has no attribute map. Any ideas how to fix this? class MyDataset(Dataset): def __init__(self, text, tags): self.sentence = text self.tags_per_token = tags def __getitem__(self, idx): sentence = self.sentence[idx] tags_per_token = self.tags_per_token[idx] #return one_text, one_label return {"text_wor...
🌐
Data Science Learner
datasciencelearner.com › home › ‘dataframe’ object has no attribute ‘map’ ( solved )
'dataframe' object has no attribute 'map' ( Solved )
September 12, 2023 - Again you are not getting the no attribute ‘map’ error when you run the code. You are getting the AttributeError ‘dataframe’ object has no attribute ‘map’ as you are applying the function using the map() function. The map() functions work only on series, not the dataframe.
🌐
GitHub
github.com › yahoo › CaffeOnSpark › issues › 193
python spark submit error:DataFrame' object has no attribute 'map' · Issue #193 · yahoo/CaffeOnSpark
June 8, 2018 - when I run CaffeOnSpark/caffe-grid/target/examples/MultiClassLogisticRegression.py,have an error: File "/usr/local/spark/python/lib/pyspark.zip/pyspark/sql/dataframe.py", line 844, in getattr AttributeError: 'DataFrame' object has no attribute 'map'
🌐
Brainly
brainly.com › computers and technology › high school › what type of error is this: 'dataframe' object has no attribute 'map'?
[FREE] What type of error is this: 'dataframe' object has no attribute 'map'? - brainly.com
November 19, 2023 - The error 'DataFrame' object has no attribute 'map' occurs when trying to use the 'map' function on a pandas DataFrame instead of a Series. To fix this, use 'applymap' for element-wise operations or 'apply' for specific rows or columns.
🌐
Databricks Community
community.databricks.com › t5 › data-engineering › attributeerror-dataframe-object-has-no-attribute › td-p › 61132
AttributeError: 'DataFrame' object has no attribut... - Databricks Community - 61132
February 19, 2024 - Hello, I have some trouble deduplicating rows on the "id" column, with the method "dropDuplicatesWithinWatermark" in a pipeline. When I run this pipeline, I get the error message: "AttributeError: 'DataFrame' object has no attribute 'dropDuplicatesWithinWatermark'" Here is part of the code: @dl...
🌐
Medium
medium.com › analytics-vidhya › no-more-confusion-between-apply-map-and-applymap-f982bb98c099
No more confusion between apply(), map() and applymap() | by Sayantanee Sen | Analytics Vidhya | Medium
May 27, 2022 - df['age']=[36,34,10,8,1] df.loc[0]=df.loc[0].map(lambda x: x.upper())AttributeError: 'numpy.int64' object has no attribute 'upper' This can be fixed with a small condition though! df.loc[0]=df.loc[0].apply(lambda x: x.upper() if type(x)==str else x) Unlike apply(), map() won’t work on a dataframe even if you have all columns of the same data type.
🌐
Stack Overflow
stackoverflow.com › questions › linked › 39535447
Hot Linked Questions - Stack Overflow
When I am working with Spark 1.6 below code is working fine: ddl = sqlContext.sql("""show create table {mytable }""".format(mytable="""mytest.my_dummytable""")) map(''.join, ddl\ .map(lambda my_row: [... ... I have to read a file which is in the HDFS and convert it to a data frame . I am doing the below steps. But unable to go ahead. Need some help. from pyspark.sql import SparkSession stock1 = spark.... ... Here I'm passing a dataframe (CV_data) to add lable points for Descision Tree def labelData(data): # label: row[end], features: row[0:end-1] return data.map(lambda row: LabeledPoint(row[-1], ...
🌐
Databricks Community
community.databricks.com › t5 › data-engineering › attributeerror-dataframe-object-has-no-attribute-rename › td-p › 28109
Solved: AttributeError: 'DataFrame' object has no attribut... - Databricks Community - 28109
January 2, 2024 - Hello, I am doing the Data Science and Machine Learning course. The Boston housing has unintuitive column names. I want to rename them, e.g. so 'zn' becomes 'Zoning'. When I run this command: df_bostonLegible = df_boston.rename({'zn':'Zoning'}, axis='columns') Then I get the error "AttributeError: '...
🌐
Python Forum
python-forum.io › thread-33991.html
AttributeError: 'DataFrame' object has no attribute 'Articles'
Purposes I want to plot feathers importance for data prediction and training and testing Running Time Error: AttributeError: 'DataFrame' object has no attribute 'Articles' Error:Traceback (most recent call last): File 'D:/Clustering/text-cluster...
🌐
Learnexams
learnexams.com › home › exams & certification › when will this error occur: `attributeerror: ‘dataframe’ object has no attribute ‘map’?
When will this error occur: `AttributeError: 'DataFrame' object has no attribute 'map'? - Learnexams
November 4, 2024 - The error AttributeError: 'DataFrame' object has no attribute 'map' occurs when trying to use the .map() function on a pandas.DataFrame object rather than a pandas.Series object. In pandas, the .map() function is designed specifically for use with a Series, not a DataFrame.