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)
1import numpy as np
2
3# 轉(zhuǎn)換某一列的數(shù)據(jù)類(lèi)型
4
5df['first_col']=pd.DataFrame(df['first_col'],dtype=np.float32)
6
7# 重新定義列名
8
9df.columns = ['first_col_1','second_col_1']
10
11print(df)
12
13# 修改部分列名
14
15df.rename(columns = {'first_col_1':'first_col_2','second_col_1':'second_col_2'},inplace = True)
16
17print(df)
18
19# 按照某個(gè)或多個(gè)字段排序,ascending = False為降序、ascending = True為升序
20
21df = df.sort_values(by=['first_col_2','second_col_2'],ascending = False)
22
23print(df)
24
25# 按照索引排序,ascending = False為降序、ascending = True為升序
26
27df = df.sort_index(axis = 0,ascending = True)
28
29print(df)
30
31# 按數(shù)據(jù)位置修改數(shù)據(jù),如下將第二行、第二列數(shù)據(jù)修改為9
32
33df.iloc[1,1] = 9
34
35print(df)
36
37# 現(xiàn)有列計(jì)算生成新的列
38
39df['third_col_2'] = df['first_col_2'] + df['second_col_2']
40
41# first_col_2 second_col_2 third_col_2
42# 0 1.0 5 6.0
43# 1 2.0 9 11.0
44# 2 3.0 7 10.0
45# 3 4.0 8 12.0
1# 重新設(shè)置索引
2
3df['index']=range(len(df['first_col']))
4
5df.set_index(df['index'])
6
7print(df)
8
9# 設(shè)置日期為索引,定義日期范圍:start開(kāi)始日期、periods數(shù)據(jù)行數(shù)
10
11date = pd.date_range(start='1/1/2021',periods=len(df['first_col']))
12
13df = df.set_index(date)
14
15print(df)
1# 定義兩個(gè)DataFrame數(shù)據(jù)
2
3df1 = pd.DataFrame(data_dict)
4
5df2 = pd.DataFrame(data_dict)
6
7# concat() 函數(shù)連接,axis=0表示當(dāng)兩個(gè)數(shù)據(jù)對(duì)象連接時(shí)存在不同的列不會(huì)生成新的列,axis=1表示會(huì)生成新的列
8
9df3=pd.concat([df1,df2],axis=0)
10
11print(df3)
12
13# 擴(kuò)展函數(shù)append() 向df1中添加df2中的所有行、最后賦值給df3
14
15df3 = df1.append(df2.loc[:])
16
17print(df3)
1# excel保存
2
3df.to_excel('/usr/data.xls')
4
5# csv 保存
6
7df.to_csv('/usr/data.csv')
8
9# 輸出字典形式
10
11dict_ = df.to_dict(orient="dict")
12
13print(dict_)
聯(lián)系客服