python里的apply,applymap和map的區別

apply,applymap和map的應用
總結:

apply?用在dataframe上,用于對row或者column進行計算;

applymap?用于dataframe上,是元素級別的操作;

map?(其實是python自帶的)用于series上,是元素級別的操作。

如:
>>> df = pd.DataFrame(np.random.randint(0,10,(4, 3)), columns=list('bde'), index=range(4))
>>> dfb  d  e
0  2  0  5
1  8  9  1
2  3  6  6
3  4  8  4
 

apply:作用在dataframe的一行或一列上

>>> f = lambda x: x.max() - x.min()
>>> df.apply(f)
b    6
d    9
e    5
dtype: int64 
>>> df.apply(f,axis=1)  # 作用在一行上
0    5
1    8
2    3
3    4
dtype: int64
>>> df.apply(f,axis=0)  # 作用在一列上,axis=0可省略
b    6
d    9
e    5
dtype: int64

applymap: 作用在dataframe的每一個元素上

>>> f2 = lambda x: x+1 if x%2==0 else x
>>> df.applymap(f2)b  d  e
0  3  1  5
1  9  9  1
2  3  7  7
3  5  9  5

?關于apply傳入多個參數:

>>> data = {'id':range(5),'value':list("abcab")}
>>> frame = pd.DataFrame(data)
>>> frameid value
0   0     a
1   1     b
2   2     c
3   3     a
4   4     b>>> def testf(x, str):
...     return x,str>>> frame["id"].apply(testf, args=("ok",))
0    (0, ok)
1    (1, ok)
2    (2, ok)
3    (3, ok)
4    (4, ok)
Name: id, dtype: object
# 注意這里args只能傳入(元組),不能是"ok"或("ok")

?

?

?

?

轉載于:https://www.cnblogs.com/cymwill/p/7577369.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391302.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391302.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391302.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

驗證曲線和學習曲線_如何擊敗技術學習曲線的怪物

驗證曲線和學習曲線Doing what I do for a living, which these days mostly involves creating technology books and courseware, I’m constantly learning new technologies. In a way, my new tech adventures are not much different than the ones most IT pros face, e…

234

234 轉載于:https://www.cnblogs.com/Forever77/p/11509588.html

SCCM PXE客戶端無法加載DP(分發點)映像

上一篇文章我們講到了一個比較典型的PXE客戶端無法找到操作系統映像的故障,今天再和大家一起分享一個關于 PXE客戶端無法加載分發點映像的問題。具體的報錯截圖如下:從報錯中我們可以看到,PXE客戶端已經成功的找到了SCCM服務器,并…

Docker 入門(2)技術實現和核心組成

1. Docker 的技術實現 Docker 的實現,主要歸結于三大技術: 命名空間 ( Namespaces )控制組 ( Control Groups )聯合文件系統 ( Union File System ) 1.1 Namespace 命名空間可以有效地幫助Docker分離進程樹、網絡接口、掛載點以及進程間通信等資源。L…

marlin 三角洲_帶火花的三角洲湖:什么和為什么?

marlin 三角洲Let me start by introducing two problems that I have dealt time and again with my experience with Apache Spark:首先,我介紹一下我在Apache Spark上的經歷反復解決的兩個問題: Data “overwrite” on the same path causing data l…

環境變量的作用

1. PATH環境變量。作用是指定命令搜索路徑,在shell下面執行命令時,它會到PATH變量所指定的路徑中查找看是否能找到相應的命令程序。我們需要把 jdk安裝目錄下的bin目錄增加到現有的PATH變量中,bin目錄中包含經常要用到的可執行文件如javac/ja…

WeWork通過向225,000個社區征稅來拼命地從Meetup.com榨取現金

Update: A few hours after I published this article, Meetup quietly added a note to the top of their announcement. They have not tweeted or done anything else to publicize this note, but some people noticed it and shared it with me.更新:在我發布本…

eda分析_EDA理論指南

eda分析Most data analysis problems start with understanding the data. It is the most crucial and complicated step. This step also affects the further decisions that we make in a predictive modeling problem, one of which is what algorithm we are going to ch…

leetcode 897. 遞增順序搜索樹(中序遍歷)

給你一棵二叉搜索樹,請你 按中序遍歷 將其重新排列為一棵遞增順序搜索樹,使樹中最左邊的節點成為樹的根節點,并且每個節點沒有左子節點,只有一個右子節點。 示例 1: 輸入:root [5,3,6,2,4,null,8,1,null…

【一針見血】 JavaScript this

JavaScript this 指向一站式解決轉載于:https://www.cnblogs.com/xueyejinghong/p/8403987.html

基于ssm框架和freemarker的商品銷售系統

項目說明 1、項目文件結構 2、項目主要接口及其實現 (1)Index: 首頁頁面:展示商品功能,可登錄或查看商品詳細信息 (2)登錄:/ApiLogin 3、dao層 數據持久化層,把商品和用戶…

c++飛揚的小鳥游戲_通過建立一個飛揚的鳥游戲來學習從頭開始

c飛揚的小鳥游戲Learn how to use Scratch 3.0 by building a flappy bird game in this course developed by Warfame. Scratch is a free programming language and online community where you can create your own interactive stories, games, and animations. Scratch is…

345

345 轉載于:https://www.cnblogs.com/Forever77/p/11512701.html

簡·雅各布斯指數第二部分:測試

In Part I, I took you through the data gathering and compilation required to rank Census tracts by the four features identified by Jane Jacobs as the foundation of a great neighborhood:在第一部分中 ,我帶您完成了根據簡雅各布斯(Jacobs Jacobs)所確定…

Docker 入門(3)Docke的安裝和基本配置

1. Docker Linux下的安裝 1.1 Docker Engine 的版本 社區版 ( CE, Community Edition ) 社區版 ( Docker Engine CE ) 主要提供了 Docker 中的容器管理等基礎功能,主要針對開發者和小型團隊進行開發和試驗企業版 ( EE, Enterprise Edition ) 企業版 ( Docker Engi…

python:單元測試框架pytest的一個簡單例子

之前一般做自動化測試用的是unitest框架,發現pytest同樣不錯,寫一個例子感受一下 test_sample.py import cx_Oracle import config from send_message import send_message from insert_cainiao_oracle import insert_cainiao_oracledef test_cainiao_mo…

mkdir命令使用范例

mkdir -p dir1/dir2/dir3/dir4 :-p 創建不存在的中間目錄mkdir -m 000 demdir :-m 000 為新創建的目錄指定權限轉載于:https://blog.51cto.com/2685141/2068162

pwa 問題_您真的需要PWA嗎? 這里有四個問題可以幫助您做出決定。

pwa 問題為什么需要PWA并不成問題。 讓我們看看為什么您可能不需要它 (Why you need a PWA is not in question. Let’s see why you may NOT need it) My inbox has been filled with questions regarding PWAs after my last two articles. 在上兩篇文章之后,我的…

利用ssh反向代理以及autossh實現從外網連接內網服務器

http://www.cnblogs.com/kwongtai/p/6903420.html轉載于:https://www.cnblogs.com/littlehb/p/7598037.html

抑郁癥損傷神經細胞嗎_使用神經網絡探索COVID-19與抑郁癥之間的聯系

抑郁癥損傷神經細胞嗎The drastic changes in our lifestyles coupled with restrictions, quarantines, and social distancing measures introduced to combat the corona virus outbreak have lead to an alarming rise in mental health issues all over the world. Social…