美文网首页我爱编程
pandas series利用apply进行批量函数操作

pandas series利用apply进行批量函数操作

作者: 不懒狮Blaise | 来源:发表于2017-09-04 18:38 被阅读0次

使用过python的朋友一般都知道,使用map操作比使用for循环操作更加的简洁美观。
pandas series也有类似map的apply函数

1、使用for循环
items = [1, 2, 3, 4, 5]
squared = []
for i in items:
    squared.append(i**2)

2、使用map加上lambada函数
items = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, items))

3、pandas使用apply
import pandas as pd
s = pd.Series([1, 2, 3, 4, 5])
s.apply(lambda x: x**2)

相关文章

网友评论

    本文标题:pandas series利用apply进行批量函数操作

    本文链接:https://www.haomeiwen.com/subject/dhyejxtx.html