7

I am trying to use applymap to my dataset to create floats into integers. But I get the "'Series' object has no attribute 'applymap'" error.

import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import pie, axis, show
from pandas import Series,DataFrame

class Dataset():
def __init__(self, input):
    self.choice = input
    self.file = 0

def read(self):
    if self.choice == ("1"):
        self.file = pd.read_csv('mycsv.csv')
        self.file.plot(kind='bar')
        print(df)

        self.file['Value'].applymap(float)

def __str__(self):
    return str(self.file)

def applymap(self):
    return self.file.applymap(float)

i = (input("Pick a DataSet= "))
df = Dataset(i)
df.read()
plt.show()
1
  • self.file['Value'] is a Series; you are calling appplymap for it while it is a DataFrame function.
    – jaumebonet
    Mar 10, 2018 at 14:08

1 Answer 1

7

As said in the documentation applymap apply a function to a whole Dataframe not to a series

Apply a function to a DataFrame that is intended to operate elementwise, i.e. like doing map(func, series) for each series in the DataFrame

To apply for function to a series use map or in your case just astype (np.float) could also work.

If you want to cast the column to float do this :

self.file['Value'].astype(np.float32)
3
  • I tried with maps. Created a df called 'Fields' that should be added to the first 3 cities. Used df['Fields']=self.file['Reference Area'].map(fields) and its saying TypeError: 'Dataset' object does not support item assignment. Mar 10, 2018 at 18:19
  • Please add sample of items in your csv file in order to get good help Mar 10, 2018 at 18:24
  • Not a screenshot , edit your answer by adding some data as text Mar 10, 2018 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.