<jupyter_start><jupyter_text>pandas的函数操作由于python本身对函数式编程的支持,以及pandas底层依赖的numpy优秀的向量化计算能力,pandas可以使用类似Universal  Function的方式向量化的求值.本文例子依然使用[iris]()数据集<jupyter_code>import pandas as pd
iris_data = pd.read_csv("source/iris.csv")
iris_data[:5]<jupyter_output><empty_output><jupyter_text>> 例:求出iris三类的信息熵<jupyter_code>import scipy as sp
slogs = lambda x:sp.log(x)*x
entropy = lambda x:sp.exp((slogs(x.sum())-x.map(slogs).sum())/x.sum())
iris_data.groupby("class").agg(entropy)<jupyter_output><empty_output><jupyter_text>广播所谓广播就是一个矢量和一个标量的运算,所有矢量中元素都被同样的操作,pandas可以支持这种操作<jupyter_code>data1 = iris_data[:10].copy()
data1
data1*10
data1["sepal_length"]*10<jupyter_output><empty_output><jupyter_text>使用numpy的universal functiion<jupyter_code>import numpy as np
np.exp(data1["sepal_length"])
f_npexp = np.frompyfunc(lambda x :np.exp(x)+1,1,1)
f_npexp(data1["sepal_length"])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1455767077267637, 'fraction_numerical': 0.01791713325867861, 'mean_word_length': 14.68421052631579, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '663140', 'license': 'MIT', 'n_tokens_mistral': 472, 'n_tokens_neox': 476, 'n_words': 33, 'path': 'src/数据清洗篇/工具介绍/pandas/pandas的函数操作.ipynb', 'repo_name': 'hsz1273327/TutorialForDataScience', 'size': '893'}