


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!

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


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

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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