Home Backend Development Python Tutorial Introduction to simple operation methods of pandas.DataFrame (create, index, add and delete) in python

Introduction to simple operation methods of pandas.DataFrame (create, index, add and delete) in python

May 29, 2018 pm 03:23 PM
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`
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

The second type: also only You can see

bb[['one','three']][:2]
Out[33]: 
 one three
22 1 3
33 4 6
Copy after login

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)
Copy after login

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
Copy after login

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
Copy after login

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"
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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

Detailed explanation of the sample code of pandas.DataFrame method of excluding specific rows in python

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

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.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

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.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

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.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

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.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles