pandas “系列”对象没有“applymap”属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49209925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'Series' object has no attribute 'applymap'
提问by DarknessPlusPlus
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.
我正在尝试对我的数据集使用 applymap 以将浮点数创建为整数。但是我收到“'Series' 对象没有属性'applymap'”错误。
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()
回答by Esptheitroad Murhabazi
As said in the documentationapplymap apply a function to a whole Dataframe not to a series
如文档中所述applymap 将函数应用于整个 Dataframe 而不是系列
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
将函数应用于旨在按元素进行操作的 DataFrame,例如为 DataFrame 中的每个系列执行 map(func, series)
To apply for function to a series use mapor in your case just astype(np.float) could also work.
要将函数应用于系列使用map或在您的情况下仅astype(np.float) 也可以工作。
If you want to cast the column to float do this :
如果要将列转换为浮动,请执行以下操作:
self.file['Value'].astype(np.float32)