Python是进行数据分析的一种出色语言,主要是因为以数据为中心的python软件包具有奇妙的生态系统。 Pandas是其中的一种,使导入和分析数据更加容易。
Dataframe.applymap()方法应用一个函数,该函数接受并返回DataFrame的每个元素的标量。
用法: DataFrame.applymap(func) 参数: func:Python function, returns a single value from a single value. 返回:Transformed DataFrame.
有关在代码中使用的CSV文件的链接,请单击此处
范例1:应用applymap()
在 DataFrame 上找到编号的函数。所有单元格中的字符数。
# importing pandas as pd
import pandas as pd
# Making data frame from the csv file
df = pd.read_csv("nba.csv")
# Printing the first 10 rows of
# the data frame for visualization
df[:10]
# Using lambda function we first convert all
# the cell to a string value and then find
# its length using len() function
df.applymap(lambda x:len(str(x)))
输出:
请注意如何将所有nan值都转换为字符串nan,并将其长度求值为3。
范例2:附加_X
在每个单元格中使用applymap()
函数。
为了追加_X
在每个单元格中,首先将每个单元格转换为字符串。
# importing pandas as pd
import pandas as pd
# Making data frame from the csv file
df = pd.read_csv("nba.csv")
# Using applymap() to append '_X'
# in each cell of the dataframe
df.applymap(lambda x:str(x) + '_X')
输出:
相关用法
- Python pandas.map()用法及代码示例
- Python Pandas Series.str.len()用法及代码示例
- Python Pandas.factorize()用法及代码示例
- Python Pandas TimedeltaIndex.name用法及代码示例
- Python Pandas dataframe.ne()用法及代码示例
- Python Pandas Series.between()用法及代码示例
- Python Pandas DataFrame.where()用法及代码示例
- Python Pandas Series.add()用法及代码示例
- Python Pandas.pivot_table()用法及代码示例
- Python Pandas Series.mod()用法及代码示例
- Python Pandas Dataframe.at[ ]用法及代码示例
- Python Pandas Dataframe.iat[ ]用法及代码示例
- Python Pandas.pivot()用法及代码示例
- Python Pandas dataframe.mul()用法及代码示例
- Python Pandas.melt()用法及代码示例
注:本文由纯净天空筛选整理自Shubham__Ranjan大神的英文原创作品 Python | Pandas dataframe.applymap()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。