Repeat simple things, repeat things insist on doing, insist on doing things with your heart; Your affirmation is I insist on the power, if this article is helpful to you, point a concern!

Related extension library
1# -*- coding: UTF-8 -*-
2
3import pandas as pd
4
5
6data_dict = {'first_col': [1.2.3.4].'second_col': [5.6.7.8]}
7
8df = pd.DataFrame(data_dict)
Copy the code
Data modification
 1import numpy as np
 2
 3Convert the data type of a column
 4
 5df['first_col']=pd.DataFrame(df['first_col'],dtype=np.float32)
 6
 7Redefine column names
 8
 9df.columns = ['first_col_1'.'second_col_1']
10
11print(df)
12
13# change some column names
14
15df.rename(columns = {'first_col_1':'first_col_2'.'second_col_1':'second_col_2'},inplace = True)
16
17print(df)
18
19Ascending = False indicates descending order and True indicates ascending order
20
21df = df.sort_values(by=['first_col_2'.'second_col_2'],ascending = False)
22
23print(df)
24
25Ascending = False indicates descending order and True indicates ascending order
26
27df = df.sort_index(axis = 0,ascending = True)
28
29print(df)
30
31Select * from row 2; select * from row 2; select * from row 2
32
33df.iloc[1.1] = 9
34
35print(df)
36
37Existing columns are computed to generate new columns
38
39df['third_col_2'] = df['first_col_2'] + df['second_col_2']
40
41# first_col_2 second_col_2 third_col_2
425
43# 1 2.0 9 11.0
44# 2 3.0 7 10.0
45# 3 4.0 8 12.0
Copy the code
The index set
 1Reset index
 2
 3df['index'] =range(len(df['first_col']))
 4
 5df.set_index(df['index'])
 6
 7print(df)
 8
 9Use either the start date or the periods data line
10
11date = pd.date_range(start='1/1/2021',periods=len(df['first_col']))
12
13df = df.set_index(date)
14
15print(df)
Copy the code
Data connection and composition
 1Define two DataFrame data
 2
 3df1 = pd.DataFrame(data_dict)
 4
 5df2 = pd.DataFrame(data_dict)
 6
 7# concat() function join,axis=0 means that no new column will be generated when two data objects are joined if there are different columns,axis=1 means that new columns will be generated
 8
 9df3=pd.concat([df1,df2],axis=0)
10
11print(df3)
12
13The extension function append() adds all lines from DF2 to df1 and assigns them to df3
14
15df3 = df1.append(df2.loc[:])
16
17print(df3)
Copy the code
DataFrame output
 1# excel save
 2
 3df.to_excel('/usr/data.xls')
 4
 5# CSV save
 6
 7df.to_csv('/usr/data.csv')
 8
 9Output dictionary form
10
11dict_ = df.to_dict(orient="dict")
12
13print(dict_)
Copy the code

More exciting things to come to wechat public account “Python Concentration Camp”, focusing on Python technology stack, information acquisition, communication community, dry goods sharing, looking forward to your joining ~