It's estimated that 80% of pandas people have to hang up!

Liu Zaoqi 2021-01-21 09:59:45
estimated pandas people hang


Hello everyone , I am a Get up early , Today I'd like to share with you pandas in apply A trick of functions .

Pandas in apply function , Widely applied , Today I want to share with you a skill of using , Use apply take dataframe The content of list Split the columns of into multiple columns .

Before splitting Data of , As shown in the red mark below :

After break up , As shown in the figure below :

In this case ,Lemon The data used comes from akshare , Before the start , To introduce the relevant package :

import pandas as pd
import akshare as ak

Lemon Two used Python The version information of the library is as follows :

print(ak.__version__)
0.7.22
print(pd.__version__)
1.1.4

First from akshare Get the data you need , Divided into two steps , First step It's a list of fund codes , as follows :

df = ak.fund_em_fund_name().head(20).tail(5)
df = df[[' Fund code ',' Fund abbreviation ']]
print(df)

The second step Is to obtain the net value data and net value date of the fund , Get... Through a custom function , The custom functions are as follows :

# Get the net value of the fund unit and the net value date
def get_mutual_fund(code):
df = ak.fund_em_open_fund_info(fund=code, indicator=" Unit net worth trend ")
df.columns = [' Net value date ', ' Unit net worth ', 'equityReturn', 'unitMoney']
df[' Net value date '] = pd.to_datetime(df[' Net value date '])
df = df.sort_values(' Net value date ',ascending=False)
unit_equity = df.head(1)[' Unit net worth '].values[0]
date_latest = df.head(1)[' Net value date '].values[0]
return [unit_equity, date_latest]

For this custom function , stay pandas Use apply To apply custom functions , This is the use of apply It's a common method of , as follows :

# Get the latest unit net value and net value date of the fund
df['tmp'] = df[' Fund code '].apply(get_mutual_fund)
print(df)

The obtained data screenshot is as follows :

In the picture above tmp Column , That's what we need to deal with this time .

There are many ways to deal with it , here Lemon Use pandas Medium apply To deal with it , relatively speaking , It's also a convenient way .

stay apply Function , Use pd.Series We can achieve our goal .

# Separate unit net value and net value date
df[[' The latest net unit value ',' Net value date ']] = df['tmp'].apply(pd.Series)
df = df.drop('tmp',axis=1)
print(df)

give the result as follows :

Seemingly complex problems , It's also very convenient to solve , Isn't it easy , Use it quickly .

-END-

This article is from WeChat official account. - Get up early Python(zaoqi-python)

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the yunjia_community@tencent.com Delete .

Original publication time : 2021-01-12

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[Liu Zaoqi]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/01/20210121095345061b.html

  1. Python批量 png转ico
  2. 使用line_profiler对python代码性能进行评估优化
  3. 使用line_profiler对python代码性能进行评估优化
  4. Getting started with Python 3 flash in win environment
  5. Common ways to write configuration files in Python
  6. Python会在2021年死去吗? Python 3.9最终版本的回顾
  7. Python batch PNG to ICO
  8. Using line_ Profiler evaluates and optimizes the performance of Python code
  9. Using line_ Profiler evaluates and optimizes the performance of Python code
  10. Will Python die in 2021? A review of the final version of Python 3.9
  11. Python3 SMTP send mail
  12. Understanding closures in Python: getting started with closures
  13. Python日志实践
  14. Python logging practice
  15. [python opencv 计算机视觉零基础到实战] 十、图片效果毛玻璃
  16. [python opencv 计算机视觉零基础到实战] 九、模糊
  17. 10. Picture effect ground glass
  18. [Python opencv computer vision zero basis to actual combat] 9. Fuzzy
  19. 使用line_profiler對python程式碼效能進行評估優化
  20. Using line_ Profiler to evaluate and optimize the performance of Python code
  21. LeetCode | 0508. 出现次数最多的子树元素和【Python】
  22. Leetcode | 0508
  23. LeetCode | 0530. 二叉搜索树的最小绝对差【Python】
  24. LeetCode | 0515. 在每个树行中找最大值【Python】
  25. Leetcode | 0530. Minimum absolute difference of binary search tree [Python]
  26. Leetcode | 0515. Find the maximum value in each tree row [Python]
  27. 我来记笔记啦-搭建python虚拟环境
  28. Let me take notes - building a python virtual environment
  29. LeetCode | 0513. 找树左下角的值【Python】
  30. Leetcode | 0513. Find the value in the lower left corner of the tree [Python]
  31. Python OpenCV 泛洪填充,取经之旅第 21 天
  32. Python opencv flood fill, day 21
  33. Python爬虫自学系列(二)
  34. Python crawler self study series (2)
  35. 【python】身份证号码有效性检验
  36. [Python] validity test of ID number
  37. Python ORM - pymysql&sqlalchemy
  38. Python ORM - pymysql&sqlalchemy
  39. centos7 安装python3.8
  40. centos7 安装python3.8
  41. Centos7 installing Python 3.8
  42. Centos7 installing Python 3.8
  43. Django——图书管理系统(六)
  44. Django——图书管理系统(五)
  45. Django -- library management system (6)
  46. Django -- library management system (5)
  47. python批量插入数据小脚本
  48. Python batch insert data script
  49. ZoomEye-python 使用指南
  50. Zoomeye Python User's Guide
  51. 用Python写代码,一分钟搞定一天工作量,同事直呼:好家伙 - 知乎
  52. Using Python to write code, one minute to complete a day's workload, colleagues call: good guy - Zhihu
  53. Python 上的可视化库——PyG2Plot
  54. Pyg2plot: a visualization library on Python
  55. Python 上的可视化库——PyG2Plot
  56. Python实用代码-无限级分类树状结构生成算法
  57. Pyg2plot: a visualization library on Python
  58. Python utility code - infinite classification tree structure generation algorithm
  59. 奇技淫巧,还是正统功夫?Python推导式最全用法
  60. Pandas 的这个知识点,估计 80% 的人都得挂!