網站首頁 編程語言 正文
1、apply用法
apply在pandas里非常好用的,那在pyodps里如何去使用,還是有一些區別的,在pyodps中要對一行數據使用自定義函數,可以使用?apply?方法,axis 參數必須為 1,表示在行上操作。
apply?的自定義函數接收一個參數,為上一步 Collection 的一行數據,用戶可以通過屬性、或者偏移取得一個字段的數據。
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 時,表示返回結果為Sequence,否則返回結果為Collection。?names
和?types
參數分別指定返回的Sequence或Collection的字段名和類型。 如果類型不指定,將會默認為string類型。
在?apply?的自定義函數中,reduce 為 False 時,也可以使用?yield
關鍵字來返回多行結果。
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
我們也可以在函數上來注釋返回的字段和類型,這樣就不需要在函數調用時再指定。
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
如果想調用 ODPS 上已經存在的 UDTF,則函數指定為函數名即可。
iris['name', 'sepallength'].apply('your_func', axis=1, names=['name2', 'sepallength2'], types=['string', 'float'])
使用?apply?對行操作,且?reduce
為 False 時,可以使用?并列多行輸出?與已有的行結合,用于后續聚合等操作。
并列多行輸出:
對于 list 及 map 類型的列,explode 方法會將該列轉換為多行輸出。使用 apply 方法也可以輸出多行。 為了進行聚合等操作,常常需要將這些輸出和原表中的列合并。此時可以使用 DataFrame 提供的并列多行輸出功能, 寫法為將多行輸出函數生成的集合與原集合中的列名一起映射。
并列多行輸出的例子如下:
>>> 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
如果多行輸出方法對某個輸入不產生任何輸出,默認輸入行將不在最終結果中出現。如果需要在結果中出現該行,可以設置?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為分組列,然后取出分組后每組的第一條數據 df_stu_frist_course = df_stu_course.groupby('student_id').first()
2、取分組排序后的第一條數據
然而pyodps中卻很坑爹,沒有什么first,只能自己想辦法。這里我又添加了一個排序
例如:
首先用student_id進行分組,然后用student_id和gmt_create進行排序,最后用窗口函數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分為一組的數據的其他列數據都改為排序后的第一個值, 也就是說原df_inst_stu_cou還沒有分組,只是添加了分組的后取出第一個值的一列,所以我們要以student_id分組去重。
所以我們只要再以student_id分組,然后用聚合函數cat將其他所有的列按照行進行連接(這里我的連接符選擇了逗號),然后在map函數中用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
相關推薦
- 2022-11-23 pytorch?tensorboard可視化的使用詳解_python
- 2022-09-03 詳解.NET主流的幾款重量級?ORM框架_實用技巧
- 2022-08-26 C++超集C++/CLI模塊的基本類型_C 語言
- 2023-10-12 antd3升級到antd4(已解決所有報錯)
- 2022-07-22 Maven項目編譯運行后target/classes目錄下沒有xml和properties文件
- 2022-12-29 Python利用卡方Chi特征檢驗實現提取關鍵文本特征_python
- 2022-07-18 SQL?Server中日期時間函數的用法詳解_MsSql
- 2022-08-21 caffe的python接口caffemodel參數及特征抽取示例_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支