程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Pandas uses pipe to integrate processes

編輯:Python

pandas Use pipe Official documents of :https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.pipe.html

Sample code

import pandas as pd
def drop_duplicates(df, column_name):
""" Delete """
return df.drop_duplicates(subset=column_name)
def add_new_column(dataframe, new_column={
}):
""" Add new data """
for key, value in new_column.items():
dataframe[key] = value
return dataframe
def main():
dataframe = pd.DataFrame({

"a": [1, 2, 3, 4, 4],
'b': [1, None, None, 4, 4],
})
df_processed = (
dataframe
.pipe(drop_duplicates, column_name=['a'])
.pipe(add_new_column, new_column={
"c": [1, 2, 3, 4]})
)
print(df_processed)
if __name__ == '__main__':
main()

The result is as follows :

 a b c
0 1 1.0 1
1 2 NaN 2
2 3 NaN 3
3 4 4.0 4

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved