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

Answer from Arran Duff 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

python - pandas 'DataFrame' object has no attribute 'map' - Stack Overflow
I have two df - df_a and df_b, # df_a number cur code 1000 USD 700 2000 USD 800 3000 USD 900 # df_b number amount deletion code 1000 0.0 L 70... More on stackoverflow.com
🌐 stackoverflow.com
August 8, 2018
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
python - 'DataFrame' object has no attribute 'map' - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
May 31, 2020
🌐
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 mlres...
Author   joaopfonseca
Top answer
1 of 2
1

You get that result because when you apply() the extractGrammar() function to your DataFrame, it passes each row of the DataFrame to the function. Then when you access the ['POS Tag'] column, it is not returning that entire Series, but rather the contents of that POS Tag cell for that row, which is a list. Lists do not have a map method. If you are trying to count the occurrences of the second element of each tuple in the POS Tag column, you could try the following:

tag_count_data = Counter([x[1] for x in email['POS Tag']])

This will give you a Counter of the second elements of the tags for that individual row.

2 of 2
0

In order to the df with the tags that I'd posted on the question and based on the kind guidance of LiamFiddler, I later on proceeded with:

  1. Turning Counter objects into a dict using dict()
  2. I turned dict into a Series,
  3. I set column values to be the column names based on this answer
  4. and then went on to select the tags that I need for my dataDrame.
def extractGrammar(email): 
   # Updated calculate the tags I need 
   tag_count_data = Counter([x[1] for x in email['POS_Tag']])
  
   #Convert the Counter object to dict
   tag_count_dict = dict(tag_count_data)

   #Turning dict into Series
   email_tag = pd.DataFrame(pd.Series(tag_count_dict).fillna(0).rename_axis('Tag'))
   email_tag = email_tag.reset_index()

   #use set_index to set Tag column values to be column names
   email_tag= email_tag.set_index("Tag").T.reset_index(drop=True).rename_axis(None, axis=1) 
   
   #select Tags that I need
   pos_columns = ['PRP','MD','JJ','JJR','JJS','RB','RBR','RBS', 'NN', 'NNS','VB', 'VBS', 'VBG','VBN','VBP','VBZ']
   for pos in pos_columns:
     if pos not in email_tag.columns:
       email_tag[pos] = 0

   email_tag = email_tag[pos_columns] 

   return email_tag

Find elsewhere
🌐
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 › 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 ...
Top answer
1 of 2
1

upper and lower dataframes have two columns called Date. You are extracting both by using upper['Date'].

Solution: Rename at least one of the columns to sth different than date and than apply your function to each column seperately.

See https://stackoverflow.com/a/54608016/6646710 for further details.

2 of 2
0
  1. Python code which returns a line graph of the record high and record low temperatures by day of the year over the period 2005-2014. The area between the record high and record low temperatures for each day should be shaded.
  2. Then, overlay a scatter of the 2015 data for any points (highs and lows) for which the ten year record (2005-2014) record high or record low was broken in 2015.
  3. Remove leap year dates (i.e. 29th February).

    from datetime import datetime
    import pandas as pd
    import matplotlib.pyplot as plt
    
    pd.set_option("display.max_rows",None,"display.max_columns",None)
    data = pd.read_csv('data/C2A2_data/BinnedCsvs_d400/fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv') 
    newdata = data[(data['Date'] >= '2005-01-01') & (data['Date'] <= '2014-12-12')]
    datamax = newdata[newdata['Element']=='TMAX']
    datamin = newdata[newdata['Element']=='TMIN']
    datamax['Date'] = pd.to_datetime(datamax['Date'])
    datamin['Date'] = pd.to_datetime(datamin['Date'])
    datamax["day_of_year"] = datamax["Date"].dt.dayofyear
    datamax = datamax.groupby('day_of_year').max()
    datamin["day_of_year"] = datamin["Date"].dt.dayofyear
    datamin = datamin.groupby('day_of_year').min()
    datamax = datamax.reset_index()
    datamin = datamin.reset_index()
    datamin['Date'] = datamin['Date'].dt.strftime('%Y-%m-%d')
    datamax['Date'] = datamax['Date'].dt.strftime('%Y-%m-%d')
    datamax = datamax[~datamax['Date'].str.contains("02-29")]
    datamin = datamin[~datamin['Date'].str.contains("02-29")]
    
    breakoutdata = data[(data['Date']  > '2014-12-31')]
    datamax2015 = breakoutdata[breakoutdata['Element']=='TMAX']
    datamin2015 = breakoutdata[breakoutdata['Element']=='TMIN']
    datamax2015['Date'] = pd.to_datetime(datamax2015['Date'])
    datamin2015['Date'] = pd.to_datetime(datamin2015['Date'])
    datamax2015["day_of_year"] = datamax2015["Date"].dt.dayofyear
    datamax2015 = datamax2015.groupby('day_of_year').max()
    datamin2015["day_of_year"] = datamin2015["Date"].dt.dayofyear
    datamin2015 = datamin2015.groupby('day_of_year').min()
    datamax2015 = datamax2015.reset_index()
    datamin2015 = datamin2015.reset_index()
    datamin2015['Date'] = datamin2015['Date'].dt.strftime('%Y-%m-%d')
    datamax2015['Date'] = datamax2015['Date'].dt.strftime('%Y-%m-%d')
    datamax2015 = datamax2015[~datamax2015['Date'].str.contains("02-29")]
    datamin2015 = datamin2015[~datamin2015['Date'].str.contains("02-29")]
    
    dataminappend = datamin2015.join(datamin,on="day_of_year",rsuffix="_new")
    lower = dataminappend.loc[dataminappend["Data_Value_new"]>dataminappend["Data_Value"]]
    datamaxappend = datamax2015.join(datamax,on="day_of_year",rsuffix="_new")
    upper = datamaxappend.loc[datamaxappend["Data_Value_new"]<datamaxappend["Data_Value"]]
    
    upper['Date'] = pd.to_datetime(upper['Date']) 
    lower['Date'] = pd.to_datetime(lower['Date']) 
    datamax['Date'] = pd.to_datetime(datamax['Date']) 
    datamin['Date'] = pd.to_datetime(datamin['Date']) 
    
    ax = plt.gca()
    plt.plot(datamax['day_of_year'],datamax['Data_Value'],color='red')
    plt.plot(datamin['day_of_year'],datamin['Data_Value'], color='blue')
    plt.scatter(upper['day_of_year'],upper['Data_Value'],color='purple')
    plt.scatter(lower['day_of_year'],lower['Data_Value'], color='cyan')
    
    plt.ylabel("Temperature (degrees C)",color='navy')
    plt.xlabel("Day of the year",color='navy',labelpad=15)
    plt.title('Record high and low temperatures by day between 2005-2014)', alpha=1.0,color='brown',y=1.08)
    ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.35),fancybox=False,labels=['Record high','Record low'])
    plt.xticks(rotation=30)
    plt.fill_between(range(len(datamax['Date'])), datamax['Data_Value'], datamin['Data_Value'],color='yellow',alpha=0.8)
    plt.show()
    
  4. I have converted the 'Date' column to a string using Datamin['Date'] = datamin['Date'].dt.strftime('%Y-%m-%d').

  5. I have then converted this back to 'datetime' format using upper['Date'] = pd.to_datetime(upper['Date'])

  6. I then used 'date of year' as the x-value.

🌐
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 - 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 by default for all dataframes in my Jupyt...
Author   info-rchitect
🌐
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...
🌐
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'
Author   chris0927
🌐
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'
🌐
GitHub
github.com › dask › dask › issues › 8624
AttributeError: 'DataFrame' object has no attribute 'name'; Various stack overflow / github suggested fixes not working · Issue #8624 · dask/dask
January 26, 2022 - I perform a pipeline of transformations on a Dask dataframe originating from dd.read_sql_table() from a view in an oracle DB. In one stage that follows many successful stages, I try to apply a simple class method to a column of type (strings). This always returns this error (main issue): { File "[redacted]/pandas/core/generic.py", line 5487, in __getattr__ return object.__getattribute__(self, name) AttributeError: 'DataFrame' object has no attribute 'name'.
Author   david-thrower