網(wǎng)站首頁 編程語言 正文
1、apply用法
apply在pandas里非常好用的,那在pyodps里如何去使用,還是有一些區(qū)別的,在pyodps中要對一行數(shù)據(jù)使用自定義函數(shù),可以使用?apply?方法,axis 參數(shù)必須為 1,表示在行上操作。
apply?的自定義函數(shù)接收一個參數(shù),為上一步 Collection 的一行數(shù)據(jù),用戶可以通過屬性、或者偏移取得一個字段的數(shù)據(jù)。
iris.apply(lambda row: row.sepallength + row.sepalwidth, axis=1, reduce=True, types='float').rename('sepaladd').head(3) sepaladd 0 8.6 1 7.9 2 7.9
reduce
為 True 時,表示返回結(jié)果為Sequence,否則返回結(jié)果為Collection。?names
和?types
參數(shù)分別指定返回的Sequence或Collection的字段名和類型。 如果類型不指定,將會默認為string類型。
在?apply?的自定義函數(shù)中,reduce 為 False 時,也可以使用?yield
關(guān)鍵字來返回多行結(jié)果。
iris.count() 150 def handle(row): yield row.sepallength - row.sepalwidth, row.sepallength + row.sepalwidth yield row.petallength - row.petalwidth, row.petallength + row.petalwidth iris.apply(handle, axis=1, names=['iris_add', 'iris_sub'], types=['float', 'float']).count() 300
我們也可以在函數(shù)上來注釋返回的字段和類型,這樣就不需要在函數(shù)調(diào)用時再指定。
from odps.df import output @output(['iris_add', 'iris_sub'], ['float', 'float']) def handle(row): yield row.sepallength - row.sepalwidth, row.sepallength + row.sepalwidth yield row.petallength - row.petalwidth, row.petallength + row.petalwidth iris.apply(handle, axis=1).count() 300
也可以使用 map-only 的 map_reduce,和 axis=1 的apply操作是等價的。
iris.map_reduce(mapper=handle).count() 300
如果想調(diào)用 ODPS 上已經(jīng)存在的 UDTF,則函數(shù)指定為函數(shù)名即可。
iris['name', 'sepallength'].apply('your_func', axis=1, names=['name2', 'sepallength2'], types=['string', 'float'])
使用?apply?對行操作,且?reduce
為 False 時,可以使用?并列多行輸出?與已有的行結(jié)合,用于后續(xù)聚合等操作。
并列多行輸出:
對于 list 及 map 類型的列,explode 方法會將該列轉(zhuǎn)換為多行輸出。使用 apply 方法也可以輸出多行。 為了進行聚合等操作,常常需要將這些輸出和原表中的列合并。此時可以使用 DataFrame 提供的并列多行輸出功能, 寫法為將多行輸出函數(shù)生成的集合與原集合中的列名一起映射。
并列多行輸出的例子如下:
>>> df id a b 0 1 [a1, b1] [a2, b2, c2] 1 2 [c1] [d2, e2] >>> df[df.id, df.a.explode(), df.b] id a b 0 1 a1 [a2, b2, c2] 1 1 b1 [a2, b2, c2] 2 2 c1 [d2, e2] >>> df[df.id, df.a.explode(), df.b.explode()] id a b 0 1 a1 a2 1 1 a1 b2 2 1 a1 c2 3 1 b1 a2 4 1 b1 b2 5 1 b1 c2 6 2 c1 d2 7 2 c1 e2
如果多行輸出方法對某個輸入不產(chǎn)生任何輸出,默認輸入行將不在最終結(jié)果中出現(xiàn)。如果需要在結(jié)果中出現(xiàn)該行,可以設(shè)置?keep_nulls=True
。
此時,與該行并列的值將輸出為空值:
>>> df
? ?id ? ? ? ? a
0 ? 1 ?[a1, b1]
1 ? 2 ? ? ? ?[]
>>> df[df.id, df.a.explode()]
? ?id ? a
0 ? 1 ?a1
1 ? 1 ?b1
>>> df[df.id, df.a.explode(keep_nulls=True)]
? ?id ? ? a
0 ? 1 ? ?a1
1 ? 1 ? ?b1
2 ? 2 ?None
from odps.df import output @output(['iris_add', 'iris_sub'], ['float', 'float']) def handle(row): yield row.sepallength - row.sepalwidth, row.sepallength + row.sepalwidth yield row.petallength - row.petalwidth, row.petallength + row.petalwidth iris[iris.category, iris.apply(handle, axis=1)]
pyodps中有很多本來在pandas中一個API解決的東西卻要想半天才能搞定。
pandas中在groupby后只要用first就可以去出分組后的第一行。
例如:
# 以student_id為分組列,然后取出分組后每組的第一條數(shù)據(jù) df_stu_frist_course = df_stu_course.groupby('student_id').first()
2、取分組排序后的第一條數(shù)據(jù)
然而pyodps中卻很坑爹,沒有什么first,只能自己想辦法。這里我又添加了一個排序
例如:
首先用student_id進行分組,然后用student_id和gmt_create進行排序,最后用窗口函數(shù)nth_value取分組中的第一個值并改名first_course_id, 并將其他字段輸出
df_group = df.groupby('student_id') df_inst_stu_cou = df_inst_stu_cou['student_id', df_group.sort(['student_id', 'gmt_create'], ascending=[True, False]).course_id.nth_value(0).rename('first_course_id')]
但是這是并不是取出第一行,而是將所有以student_id分為一組的數(shù)據(jù)的其他列數(shù)據(jù)都改為排序后的第一個值, 也就是說原df_inst_stu_cou還沒有分組,只是添加了分組的后取出第一個值的一列,所以我們要以student_id分組去重。
所以我們只要再以student_id分組,然后用聚合函數(shù)cat將其他所有的列按照行進行連接(這里我的連接符選擇了逗號),然后在map函數(shù)中用split分割成列表取第一個即可
df = df.groupby('student_id').agg(df.first_course_id.cat(sep=',').rename('first_course_ids')) df['first_course_name'] = df.first_course_names.map(lambda x: x.split(',')[0], 'string')
原文鏈接:https://juejin.cn/post/7098604886278799396
相關(guān)推薦
- 2024-01-09 bigdecimal保留兩位小數(shù)
- 2022-06-25 JQuery選擇器用法詳解_jquery
- 2023-01-15 C++實現(xiàn)自定義撤銷重做功能的示例代碼_C 語言
- 2022-08-03 python庫Tsmoothie模塊數(shù)據(jù)平滑化異常點抓取_python
- 2023-12-25 Spring @Cacheable緩存注解
- 2022-06-23 Python基于鏈接表實現(xiàn)無向圖最短路徑搜索_python
- 2022-07-21 C語言全面細致講解單雙精度float與double的使用方法_C 語言
- 2022-11-17 C#?函數(shù)返回多個值的方法詳情_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支