Add rows and columns, modify, delete, filter, judge elements and transpose operations in pandas dataframe

Yuan Xiaofeng 2021-01-21 17:35:04
add rows columns modify delete


1) Specify row index and column index labels

    index Properties can be specified DataFrame Index array in structure ,  columns Property to specify the row containing the column name ,

    While using name attribute , Through the analysis of a DataFrame The example goes on df Set up ( df.index.name and df.columns.name) You can for DataFrame Structure specifies row index labels and column index labels .

    for example , Specify row index label and column index label for product price list , The sample code is as follows :

In [24]: df.index.name = 'id'
In [25]: df.columns.name = 'item
In [26]: df
Out[26]:
item product price
id
0 The TV 2300.0
1 Air conditioner 1980.0
2 Washing machine 780.0
3 The computer NaN

2) Add a list of elements

by DataFrame Instance to add a list of elements is to specify DataFrame The name of the new column of the instance , And assign a value to it . for example , Add a discount rate column to the product price , The sample code is as follows :

In [27]: df['discount']=[0.9,0.85,0.95,1]
In [28]: df
Out[28]:
item product price discount
id
0 The TV 2300.0 0.90
1 Air conditioner 1980.0 0.85
2 Washing machine 780.0 0.95
3 The computer NaN 1.00

Or use insert() Function to add a list of elements at the specified location . for example , After the discount rate column , Add a list of prices (“price”) Multiply by the discount rate (“discount”) The actual price of (“Actual_price”) Column , The sample code is as follows :

In [29]: df.insert(3,'Actual_price',df['price']*df['discount'])
In [30]: df
Out[30]:
item product price discount Actual_price
id
0 The TV 2300.0 0.90 2070.0
1 Air conditioner 1980.0 0.85 1683.0
2 Washing machine 780.0 0.95 741.0
3 The computer NaN 1.00 NaN

3) Add a line of elements

by DataFrame The way to add a row of elements to an instance is to use loc The attribute is DataFrame Add a new line to the instance , And assign a value to this line . for example , Add the price line of a mobile phone product to the product price , The sample code is as follows :

In [31]: df.loc['add_row'] = [' mobile phone ',1900,1,1900]
In [32]: df
Out[32]:
item product price discount Actual_price
id
0 The TV 2300.0 0.90 2070.0
1 Air conditioner 1980.0 0.85 1683.0
2 Washing machine 780.0 0.95 741.0
3 The computer NaN 1.00 NaN
add_row mobile phone 1900.0 1.00 1900.0

4) Modify a line of elements

modify DataFrame A row of elements in an object , Just use loc Attribute specifies DataFrame The row index in the instance , And assign a value to this line . for example , Modify the data of the computer line in the product price , The sample code is as follows :

In [33]: df.loc[3] = [' The computer ',4500,1,4500]
In [34]: df
Out[34]:
item product price iscount Actual_price
id
0 The TV 2300.0 0.90 2070.0
1 Air conditioner 1980.0 0.85 1683.0
2 Washing machine 780.0 0.95 741.0
3 The computer 4500.0 1.00 4500.0
add_row mobile phone 1900.0 1.00 1900.0

5) Modify a list of elements or an element

modify DataFrame A list of elements in an instance , Just specify DataFrame The column name in the instance , Store a list of elements to be updated in the array , Then assign the array to this column .
for example , Change the price in the product price to the new price [3000,2300,560,5600], The sample code is as follows :

In [35]: df['price']=[3000,2300,560,5600,1880]
In [36]: df
Out[36]:
item product price discount Actual_price
id
0 The TV 3000 0.90 2070.0
1 Air conditioner 2300 0.85 1683.0
2 Washing machine 560 0.95 741.0
3 The computer 5600 1.00 4500.0
add_row mobile phone 1880 1.00 1900.0

Modify an element , Just select the element , Just give it a value . for example df['discount'][1]=0.96.

6) Remove elements

 6.1) Use del Command to delete a list of elements

If you want to delete all the data in an entire column , Use del command . for example , Delete the actual price column in the product price , The sample code is as follows :

In [37]: del df['Actual_price']
In [38]: df
Out[38]:
item product price discou
id
0 The TV 3000 0.90
1 Air conditioner 2300 0.85
2 Washing machine 560 0.95
3 The computer 5600 1.00
add_row mobile phone 1880 1.00

6.2) Use pop() Function to delete a list of elements

  pop() Function to delete the selected column from the original data block , The column is no longer retained in the original block . for example , Use pop() Function to delete the discount rate column , The sample code is as follows :

In [39]: df.pop('discount')
Out[39]: id
0 0.90
1 0.85
2 0.95 3 1.00
add_row 1.00
Name: discount, dtype: float64

 6.3) Use drop() Function to delete a column of elements or a row of elements

stay drop() There are two parameters in the function , One parameter is axis, When parameters axis=1 when , Then delete the column elements ; When axis=0 when , Then delete the line element . Another parameter is inplace, When inplace by True when ,drop() Function to perform internal deletion , No value returned , The original data has changed ; When inplace by False when , The original data will not change , Just output new variables, delete .

for example , To add 1 Column discount rate column , And then use drop() Function to set parameters axis=1 and inplace=True Delete column element of discount rate , The sample code is as follows :

In [40]: df['discount']=0.94
In [41]: df.drop(['discount'],axis=1,inplace=True)

If you want to delete a line , Is set axis=0, And specify the row index or row label to delete . for example , Delete row label as add_row A line of elements , The sample code is as follows :

In [42]: df.drop(['add_row'],axis=0,inplace=True)

If you want to delete multiple lines , In addition to setting the axis=0 Outside , Also specify the row index or row label to delete , for example , Delete the first 1 Xing He 3 Row element , The sample code is as follows :

In [43]: df.drop([0,2],axis=0,inplace=True)

7) Filter elements

about DataFrame object , You can also filter elements by specifying criteria . for example , The price of the product is higher than 2000 Meta product information , The sample code is as follows :

In [44]: df[df['price']>2000]
Out[44]:
item product price
id
0 The TV 3000
1 Air conditioner 2300
3 The computer 5600

for example , All the elements in the product price are less than 2000 Meta product information , The sample code is as follows :

In [45]: df[df<2000]
Out[45]:
item product price
id
0 The TV NaN
1 Air conditioner NaN
2 Washing machine 560
3 The computer NaN

Back to DataFrame Object contains only numbers that satisfy the condition , The position of each element remains unchanged , Other elements that do not meet the criteria are replaced with NaN.

8) Determine whether an element exists

Use isin() Function to determine whether a given list of elements is contained in DataFrame In structure , If the given element is contained in the data structure ,isin() The function returns True, Otherwise, the return is False. Use this function to filter DataFrame Data in column .

For example, to determine whether there is “ The computer ” and 2300 These two elements , And return the element that satisfies the condition , The sample code is as follows :

In [46]: df[df.isin([' The computer ',2300])]
Out[46]:
item product price
id
0 NaN NaN
1 NaN 2300.0
2 NaN NaN
3 The computer NaN

9) DataFrame Transposition

DataFrame The data structure is similar to the table data structure , When processing table data , Transpose operations are often used , Change column to row , Rows become columns .pandas Provides a simple transpose method , By calling T Attribute gets DataFrame Transpose form of object .

For example, transpose the product price data structure , The sample code is as follows :

In [47]: df.T
Out[47]:
id 0 1 2 3
item
product The TV Air conditioner Washing machine The computer
price 3000 2300 560 5600

版权声明
本文为[Yuan Xiaofeng]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/01/20210121173408302q.html

  1. Centos7 installing Python 3.8
  2. Centos7 installing Python 3.8
  3. Django——图书管理系统(六)
  4. Django——图书管理系统(五)
  5. Django -- library management system (6)
  6. Django -- library management system (5)
  7. python批量插入数据小脚本
  8. Python batch insert data script
  9. ZoomEye-python 使用指南
  10. Zoomeye Python User's Guide
  11. 用Python写代码,一分钟搞定一天工作量,同事直呼:好家伙 - 知乎
  12. Using Python to write code, one minute to complete a day's workload, colleagues call: good guy - Zhihu
  13. Python 上的可视化库——PyG2Plot
  14. Pyg2plot: a visualization library on Python
  15. Python 上的可视化库——PyG2Plot
  16. Python实用代码-无限级分类树状结构生成算法
  17. Pyg2plot: a visualization library on Python
  18. Python utility code - infinite classification tree structure generation algorithm
  19. 奇技淫巧,还是正统功夫?Python推导式最全用法
  20. Pandas 的这个知识点,估计 80% 的人都得挂!
  21. 前后端分离有什么了不起,手把手教你用Python爬下来!
  22. 在 Azure 上执行一些简单的 python 工作
  23. 推荐 :利用Python的混合集成机器学习(附链接)
  24. Cunning or orthodox Kung Fu? The most complete usage of Python derivation
  25. It's estimated that 80% of pandas people have to hang up!
  26. What's so great about the separation of front and rear ends? Hand in hand teach you to climb down with Python!
  27. Doing some simple Python work on azure
  28. Recommendation: hybrid integrated machine learning using python (link attached)
  29. Learning PPO algorithm programming from scratch (Python version)
  30. Python OpenCV 图片模糊操作 blur 与 medianBlur
  31. Python OpenCV image blur operation blur and mediablur
  32. 成功解决cv2.error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: err
  33. Cv2.error solved successfully: opencv (4.1.2) C:: (projects / opencv Python / opencv modules / imgproc / SRC)\ color.cpp:182 : err
  34. Python 中使用 virtualenv 管理虚拟环境
  35. Using virtualenv to manage virtual environment in Python
  36. 如何使用Python执行系统命令?Python学习教程!
  37. How to use Python to execute system commands? Python tutorial!
  38. 快速掌握Python中的循环技术
  39. Quickly grasp the loop technology in Python
  40. Python主流Web框架之Tornado
  41. appium+python自动化63-使用Uiautomator2报错问题解决
  42. Tornado: the mainstream Python Web Framework
  43. Appium + Python automation 63 - using uiautomator2 to solve the problem of error reporting
  44. 爬虫+django,打造个性化API接口
  45. Crawler + Django to create personalized API interface
  46. 爬虫+django,打造个性化API接口
  47. Crawler + Django to create personalized API interface
  48. C、C++、Java、PHP、Python主要应用在哪里方面?
  49. C. Where are the main applications of C + +, Java, PHP and python?
  50. Python 无限级分类树状结构生成算法 「实用代码」
  51. Python infinite classification tree structure generation algorithm "practical code"
  52. 【Azure 存储服务】Python模块(azure.cosmosdb.table)直接对表存储(Storage Account Table)做操作示例
  53. [azure storage service] Python module( azure.cosmosdb.table )Direct operation example of storage account table
  54. 【Azure 存储服务】Python模块(azure.cosmosdb.table)直接对表存储(Storage Account Table)做操作示例
  55. [azure storage service] Python module( azure.cosmosdb.table )Direct operation example of storage account table
  56. openpose c++ 配置教程 + python api
  57. Openpose C + + configuration tutorial + Python API
  58. PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二手急速响应捡垃圾平台_3(附源码持续更新)
  59. 使用python javaSerializationTools模块拼接生成 8u20 Gadget
  60. 萌新入门之python基础语法