


Introduction to simple operation methods of pandas.DataFrame (create, index, add and delete) in python
This article introduces the simple operation methods of pandas.DataFrame (creation, indexing, addition and deletion) in python, including related information on creation, indexing, addition and deletion, etc. The introduction in the article is very detailed. Friends who need it can For reference, let’s take a look below.
Preface
Recently, I have searched a lot of operation instructions on the Internet for pandas.DataFrame
, all of which are basic. operations, but the combination of these operations still takes time to correctly operate the DataFrame, and it took me a long time to adjust the bug. I will make some summaries here for the convenience of you, me and others. Friends who are interested, please come and take a look.
1. Simple operation to create DataFrame:
1. Create according to dictionary:
In [1]: import pandas as pd In [3]: aa={'one':[1,2,3],'two':[2,3,4],'three':[3,4,5]} In [4]: bb=pd.DataFrame(aa) In [5]: bb Out[5]: one three two 0 1 3 2 1 2 4 3 2 3 5 4`
The keys in the dictionary are the columns in the DataFrame, but there is no index value, so you need to set it yourself. If not set, the default is to start counting from zero.
bb=pd.DataFrame(aa,index=['first','second','third']) bb Out[7]: one three two first 1 3 2 second 2 4 3 third 3 5 4
2. Create from a multi-dimensional array
import numpy as np In [9]: del aa In [10]: aa=np.array([[1,2,3],[4,5,6],[7,8,9]]) In [11]: aa Out[11]: array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) In [12]: bb=pd.DataFrame(aa) In [13]: bb Out[13]: 0 1 2 0 1 2 3 1 4 5 6 2 7 8 9
To create from a multi-dimensional array, you need to assign columns and index to the DataFrame, otherwise it will be the default, which is ugly.
bb=pd.DataFrame(aa,index=[22,33,44],columns=['one','two','three']) In [15]: bb Out[15]: one two three 22 1 2 3 33 4 5 6 44 7 8 9
3. Create with other DataFrame
bb=pd.DataFrame(aa,index=[22,33,44],columns=['one','two','three']) bb Out[15]: one two three 22 1 2 3 33 4 5 6 44 7 8 9 cc=bb[['one','three']].copy() Cc Out[17]: one three 22 1 3 33 4 6 44 7 9
The copy here is a deep copy. Changing the value in cc cannot change the value in bb.
cc['three'][22]=5 bb Out[19]: one two three 22 1 2 3 33 4 5 6 44 7 8 9 cc Out[20]: one three 22 1 5 33 4 6 44 7 9
2. Index operation of DataFrame:
For a DataFrame, indexing is the most troublesome and error-prone.
1. Indexing one or more columns is relatively simple:
bb['one'] Out[21]: 22 1 33 4 44 7 Name: one, dtype: int32
For multiple column names, the input column names need to be stored in a list to be a collerable variable. , otherwise an error will be reported.
bb[['one','three']] Out[29]: one three 22 1 3 33 4 6 44 7 9
2. Index one record or several records:
bb[1:3] Out[27]: one two three 33 4 5 6 44 7 8 9 bb[:1] Out[28]: one two three 22 1 2 3
Note here that the colon is required, otherwise it will be an index column. .
3. Index certain records of variables in certain columns. This tortured me for a long time:
First type
bb.loc[[22,33]][['one','three']] Out[30]: one three 22 1 3 33 4 6
You cannot change the value here. You can only read the value but not write it. It may be related to the loc()
function:
bb.loc[[22,33]][['one','three']]=[[2,2],[3,6]] In [32]: bb Out[32]: one two three 22 1 2 3 33 4 5 6 44 7 8 9
The second type: also only You can see
bb[['one','three']][:2] Out[33]: one three 22 1 3 33 4 6
If you want to change the value, an error will be reported.
In [34]: bb[['one','three']][:2]=[[2,2],[2,2]] -c:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_index,col_indexer] = value instead F:\Anaconda\lib\site-packages\pandas\core\frame.py:1999: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame return self._setitem_slice(indexer, value)
The third type: you can change the value of the data! ! !
Iloc is indexed according to the number of rows and columns of data, not counting index and columns
bb.iloc[2:3,2:3] Out[36]: three 44 9 bb.iloc[1:3,1:3] Out[37]: two three 33 5 6 44 8 9 bb.iloc[0,0] Out[38]: 1
The following is the proof:
bb.iloc[0:4,0:2]=[[9,9],[9,9],[9,9]] In [45]: bb Out[45]: one two three 22 9 9 3 33 9 9 6 44 9 9 9
3. In the original Create a new column or several columns on the DataFrame
1. Use nothing. You can only create one column separately. Multiple columns are not easy to use. Personal test is invalid:
bb['new']=[2,3,4] bb Out[51]: one two three new 22 9 9 3 2 33 9 9 6 3 44 9 9 9 4 bb[['new','new2']]=[[2,3,4],[5,3,7]] KeyError: "['new' 'new2'] not in index"
The list assigned is basically assigned in the order of the given index value, but generally we need to assign the corresponding index. If you want more advanced assignments, look at the following.
2. Use a dictionary to assign values to multiple columns by index:
aa={33:[234,44,55],44:[657,77,77],22:[33,55,457]} In [58]: bb=bb.join(pd.DataFrame(aa.values(),columns=['hi','hello','ok'],index=aa.keys())) In [59]: bb Out[59]: one two three new hi hello ok 22 9 9 3 2 33 55 457 33 9 9 6 3 234 44 55 44 9 9 9 4 657 77 77
Here aa is a nested dictionary and list, which is equivalent to a record. Use keys as index name instead of the usual default column names. The purpose of matching multiple columns by index is achieved. Since the storage of dict()
is chaotic, using dict()
without assigning its index value will cause confusion in the records. This is worth noting.
4. Delete multiple columns or records:
Delete columns
bb.drop(['new','hi'],axis=1) Out[60]: one two three hello ok 22 9 9 3 55 457 33 9 9 6 44 55 44 9 9 9 77 77
Delete record
bb.drop([22,33],axis=0) Out[61]: one two three new hi hello ok 44 9 9 9 4 657 77 77
Share with you an article about summing rows and columns and adding new rows and columns in pandas.DataFrame in python. Friends who are interested can take a look.
There are many functions of DataFrame that have not been covered yet. They will be covered in the future. After reading the API on the official website, I will continue to share it. Everything is ok.
Related articles:
About pandas.DataFrame in python to sum rows and columns and add new rows and columns sample code
The above is the detailed content of Introduction to simple operation methods of pandas.DataFrame (create, index, add and delete) in python. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
