pandas模块

目录

❤  Series数据结构

Series支持NumPy模块的特性(下标)

Series支持字典的特性(标签)

Series缺失数据处理

❤  DataFrame数据结构

产生时间对象数组:date_range

❤  DataFrame属性

❤  DataFrame取值

通过columns取值

loc(通过行标签取值)

iloc(类似于numpy数组取值)

使用逻辑判断取值

❤  DataFrame值替换

❤  读取CSV文件

❤  处理丢失数据

❤  合并数据

❤  导入导出数据

读取文件导入数据

写入文件导出数据

❤  pandas读取json文件

orient参数的五种形式

❤  pandas读取sql语句


python从小白到总裁完整教程目录:https://blog.csdn.net/weixin_67859959/article/details/129328397?spm=1001.2014.3001.5502

pandas基于Numpy,可以看成是处理文本或者表格数据。pandas中有两个主要的数据结构,其中Series数据结构类似于Numpy中的一维数组,DataFrame类似于多维表格数据结构。

pandas是python数据分析的核心模块。它主要提供了五大功能:

  • 支持文件存取操作,支持数据库(sql)、html、json、pickle、csv(txt、excel)、sas、stata、hdf等。
  • 支持增删改查、切片、高阶函数、分组聚合等单表操作,以及和dict、list的互相转换。
  • 支持多表拼接合并操作。
  • 支持简单的绘图操作。
  • 支持简单的统计分析操作。

❤  Series数据结构

Series是一种类似于一维数组的对象,由一组数据和一组与之相关的数据标签(索引)组成。

Series比较像列表(数组)和字典的结合体

import numpy as np
import pandas as pd
df = pd.Series(0, index=['a', 'b', 'c', 'd'])
print(df)

a    0
b    0
c    0
d    0
dtype: int64

print(df.values)

[0 0 0 0]

print(df.index)

Index(['a', 'b', 'c', 'd'], dtype='object')

Series支持NumPy模块的特性(下标)

详解方法
从ndarray创建SeriesSeries(arr)
与标量运算df*2
两个Series运算df1+df2
索引df[0], df[[1,2,4]]
切片df[0:2]
通用函数np.abs(df)
布尔值过滤df[df>0]
arr = np.array([1, 2, 3, 4, np.nan])
print(arr)

[ 1.  2.  3.  4. nan]

df = pd.Series(arr, index=['a', 'b', 'c', 'd', 'e'])
print(df)

a    1.0
b    2.0
c    3.0
d    4.0
e    NaN
dtype: float64

print(df**2)

a     1.0
b     4.0
c     9.0
d    16.0
e     NaN
dtype: float64

print(df[0])

1.0

print(df['a'])

1.0

print(df[[0, 1, 2]])

a    1.0
b    2.0
c    3.0
dtype: float64

print(df[0:2])

a    1.0
b    2.0
dtype: float64

np.sin(df)

a    0.841471
b    0.909297
c    0.141120
d   -0.756802
e         NaN
dtype: float64

df[df > 1]

b    2.0
c    3.0
d    4.0
dtype: float64

Series支持字典的特性(标签)

详解方法
从字典创建SeriesSeries(dic),
in运算’a’ in sr
键索引sr['a'], sr[['a', 'b', 'd']]
df = pd.Series({'a': 1, 'b': 2})
print(df)

a    1
b    2
dtype: int64

print('a' in df)

True

print(df['a'])

1

Series缺失数据处理

方法详解
dropna()过滤掉值为NaN的行
fillna()填充缺失数据
isnull()返回布尔数组,缺失值对应为True
notnull()返回布尔数组,缺失值对应为False
df = pd.Series([1, 2, 3, 4, np.nan], index=['a', 'b', 'c', 'd', 'e'])
print(df)

a    1.0
b    2.0
c    3.0
d    4.0
e    NaN
dtype: float64

print(df.dropna())

a    1.0
b    2.0
c    3.0
d    4.0
dtype: float64

print(df.fillna(5))

a    1.0
b    2.0
c    3.0
d    4.0
e    5.0
dtype: float64

print(df.isnull())

a    False
b    False
c    False
d    False
e     True
dtype: bool

print(df.notnull())

a     True
b     True
c     True
d     True
e    False
dtype: bool

❤  DataFrame数据结构

DataFrame是一个表格型的数据结构,含有一组有序的列。

DataFrame可以被看做是由Series组成的字典,并且共用一个索引。

产生时间对象数组:date_range

date_range参数详解:

参数详解
start开始时间
end结束时间
periods时间长度
freq时间频率,默认为'D',可选H(our),W(eek),B(usiness),S(emi-)M(onth),(min)T(es), S(econd), A(year),…
dates = pd.date_range('20190101', periods=6, freq='M')
print(dates)

DatetimeIndex(['2019-01-31', '2019-02-28', '2019-03-31', '2019-04-30',
               '2019-05-31', '2019-06-30'],
              dtype='datetime64[ns]', freq='M')

np.random.seed(1)
arr = 10 * np.random.randn(6, 4)
print(arr)

[[ 16.24345364  -6.11756414  -5.28171752 -10.72968622]
[  8.65407629 -23.01538697  17.44811764  -7.61206901]
[  3.19039096  -2.49370375  14.62107937 -20.60140709]
[ -3.22417204  -3.84054355  11.33769442 -10.99891267]
[ -1.72428208  -8.77858418   0.42213747   5.82815214]
[-11.00619177  11.4472371    9.01590721   5.02494339]]

df = pd.DataFrame(arr, index=dates, columns=['c1', 'c2', 'c3', 'c4'])
df
c1c2c3c4
16.243454-6.117564-5.281718-10.729686
8.654076-23.01538717.448118-7.612069
3.190391-2.49370414.621079-20.601407
-3.224172-3.84054411.337694-10.998913
-1.724282-8.7785840.4221375.828152
-11.00619211.4472379.0159075.024943

❤  DataFrame属性

属性详解
dtype是查看数据类型
index查看行序列或者索引
columns查看各列的标签
values查看数据框内的数据,也即不含表头索引的数据
describe查看数据每一列的极值,均值,中位数,只可用于数值型数据
transpose转置,也可用T来操作
sort_index排序,可按行或列index排序输出
sort_values按数据值来排序
# 查看数据类型
print(df2.dtypes)

0    float64
1    float64
2    float64
3    float64
dtype: object

df
c1c2c3c4
16.243454-6.117564-5.281718-10.729686
8.654076-23.01538717.448118-7.612069
3.190391-2.49370414.621079-20.601407
-3.224172-3.84054411.337694-10.998913
-1.724282-8.7785840.4221375.828152
-11.00619211.4472379.0159075.024943
print(df.index)

DatetimeIndex(['2019-01-31', '2019-02-28', '2019-03-31', '2019-04-30',
               '2019-05-31', '2019-06-30'],
              dtype='datetime64[ns]', freq='M')

print(df.columns)

Index(['c1', 'c2', 'c3', 'c4'], dtype='object')

print(df.values)

[[ 16.24345364  -6.11756414  -5.28171752 -10.72968622]
[  8.65407629 -23.01538697  17.44811764  -7.61206901]
[  3.19039096  -2.49370375  14.62107937 -20.60140709]
[ -3.22417204  -3.84054355  11.33769442 -10.99891267]
[ -1.72428208  -8.77858418   0.42213747   5.82815214]
[-11.00619177  11.4472371    9.01590721   5.02494339]]

df.describe()
c1c2c3c4
6.0000006.0000006.0000006.000000
2.022213-5.4664247.927203-6.514830
9.58008411.1077728.70717110.227641
-11.006192-23.015387-5.281718-20.601407
-2.849200-8.1133292.570580-10.931606
0.733054-4.97905410.176801-9.170878
7.288155-2.83041413.8002331.865690
16.24345411.44723717.4481185.828152
df.T
2019-01-31 00:00:002019-02-28 00:00:002019-03-31 00:00:002019-04-30 00:00:002019-05-31 00:00:002019-06-30 00:00:00
16.2434548.6540763.190391-3.224172-1.724282-11.006192
-6.117564-23.015387-2.493704-3.840544-8.77858411.447237
-5.28171817.44811814.62107911.3376940.4221379.015907
-10.729686-7.612069-20.601407-10.9989135.8281525.024943
# 按行标签[c1, c2, c3, c4]从大到小排序
df.sort_index(axis=0)
c1c2c3c4
16.243454-6.117564-5.281718-10.729686
8.654076-23.01538717.448118-7.612069
3.190391-2.49370414.621079-20.601407
-3.224172-3.84054411.337694-10.998913
-1.724282-8.7785840.4221375.828152
-11.00619211.4472379.0159075.024943
# 按列标签[2019-01-01, 2019-01-02...]从大到小排序
df.sort_index(axis=1)
c1c2c3c4
16.243454-6.117564-5.281718-10.729686
8.654076-23.01538717.448118-7.612069
3.190391-2.49370414.621079-20.601407
-3.224172-3.84054411.337694-10.998913
-1.724282-8.7785840.4221375.828152
-11.00619211.4472379.0159075.024943
# 按c2列的值从大到小排序
df.sort_values(by='c2')
c1c2c3c4
8.654076-23.01538717.448118-7.612069
-1.724282-8.7785840.4221375.828152
16.243454-6.117564-5.281718-10.729686
-3.224172-3.84054411.337694-10.998913
3.190391-2.49370414.621079-20.601407
-11.00619211.4472379.0159075.024943

❤  DataFrame取值

df
c1c2c3c4
16.243454-6.117564-5.281718-10.729686
8.654076-23.01538717.448118-7.612069
3.190391-2.49370414.621079-20.601407
-3.224172-3.84054411.337694-10.998913
-1.724282-8.7785840.4221375.828152
-11.00619211.4472379.0159075.024943

通过columns取值

df['c2']

2019-01-31    -6.117564
2019-02-28   -23.015387
2019-03-31    -2.493704
2019-04-30    -3.840544
2019-05-31    -8.778584
2019-06-30    11.447237
Freq: M, Name: c2, dtype: float64

df[['c2', 'c3']]
c2c3
-6.117564-5.281718
-23.01538717.448118
-2.49370414.621079
-3.84054411.337694
-8.7785840.422137
11.4472379.015907

loc(通过行标签取值)

# 通过自定义的行标签选择数据
df.loc['2019-01-01':'2019-01-03']
df[0:3]
c1c2c3c4
16.243454-6.117564-5.281718-10.729686
8.654076-23.01538717.448118-7.612069
3.190391-2.49370414.621079-20.601407

iloc(类似于numpy数组取值)

df.values

array([[ 16.24345364,  -6.11756414,  -5.28171752, -10.72968622],
       [  8.65407629, -23.01538697,  17.44811764,  -7.61206901],
       [  3.19039096,  -2.49370375,  14.62107937, -20.60140709],
       [ -3.22417204,  -3.84054355,  11.33769442, -10.99891267],
       [ -1.72428208,  -8.77858418,   0.42213747,   5.82815214],
       [-11.00619177,  11.4472371 ,   9.01590721,   5.02494339]])

# 通过行索引选择数据
print(df.iloc[2, 1])

-2.493703754774101

df.iloc[1:4, 1:4]
c2c3c4
-23.01538717.448118-7.612069
-2.49370414.621079-20.601407
-3.84054411.337694-10.998913

使用逻辑判断取值

df[df['c1'] > 0]
c1c2c3c4
16.243454-6.117564-5.281718-10.729686
8.654076-23.01538717.448118-7.612069
3.190391-2.49370414.621079-20.601407
df[(df['c1'] > 0) & (df['c2'] > -8)]
c1c2c3c4
16.243454-6.117564-5.281718-10.729686
3.190391-2.49370414.621079-20.601407

❤  DataFrame值替换

df
c1c2c3c4
16.243454-6.117564-5.281718-10.729686
8.654076-23.01538717.448118-7.612069
3.190391-2.49370414.621079-20.601407
-3.224172-3.84054411.337694-10.998913
-1.724282-8.7785840.4221375.828152
-11.00619211.4472379.0159075.024943
df.iloc[0:3, 0:2] = 0
df
c1c2c3c4
0.0000000.000000-5.281718-10.729686
0.0000000.00000017.448118-7.612069
0.0000000.00000014.621079-20.601407
-3.224172-3.84054411.337694-10.998913
-1.724282-8.7785840.4221375.828152
-11.00619211.4472379.0159075.024943
df['c3'] > 10

2019-01-31    False
2019-02-28     True
2019-03-31     True
2019-04-30     True
2019-05-31    False
2019-06-30    False
Freq: M, Name: c3, dtype: bool

# 针对行做处理
df[df['c3'] > 10] = 100
df
c1c2c3c4
0.0000000.000000-5.281718-10.729686
100.000000100.000000100.000000100.000000
100.000000100.000000100.000000100.000000
100.000000100.000000100.000000100.000000
-1.724282-8.7785840.4221375.828152
-11.00619211.4472379.0159075.024943
# 针对行做处理
df = df.astype(np.int32)
df[df['c3'].isin([100])] = 1000
df
c1c2c3c4
00-5-10
1000100010001000
1000100010001000
1000100010001000
-1-805
-111195

❤  读取CSV文件

import pandas as pd
from io import StringIO
test_data = '''
5.1,,1.4,0.2
4.9,3.0,1.4,0.2
4.7,3.2,,0.2
7.0,3.2,4.7,1.4
6.4,3.2,4.5,1.5
6.9,3.1,4.9,
,,,
'''

test_data = StringIO(test_data)
df = pd.read_csv(test_data, header=None)
df.columns = ['c1', 'c2', 'c3', 'c4']
df
c1c2c3c4
5.1NaN1.40.2
4.93.01.40.2
4.73.2NaN0.2
7.03.24.71.4
6.43.24.51.5
6.93.14.9NaN
NaNNaNNaNNaN

❤  处理丢失数据

df.isnull()
c1c2c3c4
FalseTrueFalseFalse
FalseFalseFalseFalse
FalseFalseTrueFalse
FalseFalseFalseFalse
FalseFalseFalseFalse
FalseFalseFalseTrue
TrueTrueTrueTrue

# 通过在isnull()方法后使用sum()方法即可获得该数据集某个特征含有多少个缺失值
print(df.isnull().sum())
c1    1
c2    2
c3    2
c4    2
dtype: int64
# axis=0删除有NaN值的行
df.dropna(axis=0)
c1c2c3c4
4.93.01.40.2
7.03.24.71.4
6.43.24.51.5
# axis=1删除有NaN值的列
df.dropna(axis=1)
# 删除全为NaN值得行或列
df.dropna(how='all')
c1c2c3c4
5.1NaN1.40.2
4.93.01.40.2
4.73.2NaN0.2
7.03.24.71.4
6.43.24.51.5
6.93.14.9NaN
# 删除行不为4个值的
df.dropna(thresh=4)
c1c2c3c4
4.93.01.40.2
7.03.24.71.4
6.43.24.51.5
# 删除c2中有NaN值的行
df.dropna(subset=['c2'])
c1c2c3c4
4.93.01.40.2
4.73.2NaN0.2
7.03.24.71.4
6.43.24.51.5
6.93.14.9NaN
# 填充nan值
df.fillna(value=10)
c1c2c3c4
5.110.01.40.2
4.93.01.40.2
4.73.210.00.2
7.03.24.71.4
6.43.24.51.5
6.93.14.910.0
10.010.010.010.0

❤  合并数据

df1 = pd.DataFrame(np.zeros((3, 4)))
df1
0123
0.00.00.00.0
0.00.00.00.0
0.00.00.00.0
df2 = pd.DataFrame(np.ones((3, 4)))
df2
0123
1.01.01.01.0
1.01.01.01.0
1.01.01.01.0
# axis=0合并列
pd.concat((df1, df2), axis=0)
0123
0.00.00.00.0
0.00.00.00.0
0.00.00.00.0
1.01.01.01.0
1.01.01.01.0
1.01.01.01.0
# axis=1合并行
pd.concat((df1, df2), axis=1)
01230123
0.00.00.00.01.01.01.01.0
0.00.00.00.01.01.01.01.0
0.00.00.00.01.01.01.01.0
# append只能合并列
df1.append(df2)
0123
0.00.00.00.0
0.00.00.00.0
0.00.00.00.0
1.01.01.01.0
1.01.01.01.0
1.01.01.01.0

❤  导入导出数据

使用df = pd.read_excel(filename)读取文件,使用df.to_excel(filename)保存文件。

读取文件导入数据

读取文件导入数据函数主要参数:

参数详解
sep指定分隔符,可用正则表达式如'\s+'
header=None指定文件无行名
name指定列名
index_col指定某列作为索引
skip_row指定跳过某些行
na_values指定某些字符串表示缺失值
parse_dates指定某些列是否被解析为日期,布尔值或列表
df = pd.read_excel(filename)
df = pd.read_csv(filename)

写入文件导出数据

写入文件函数的主要参数:

参数详解
sep分隔符
na_rep指定缺失值转换的字符串,默认为空字符串
header=False不保存列名
index=False不保存行索引
cols指定输出的列,传入列表
df.to_excel(filename)

❤  pandas读取json文件

strtext = '[{"ttery":"min","issue":"20130801-3391","code":"8,4,5,2,9","code1":"297734529","code2":null,"time":1013395466000},\
{"ttery":"min","issue":"20130801-3390","code":"7,8,2,1,2","code1":"298058212","code2":null,"time":1013395406000},\
{"ttery":"min","issue":"20130801-3389","code":"5,9,1,2,9","code1":"298329129","code2":null,"time":1013395346000},\
{"ttery":"min","issue":"20130801-3388","code":"3,8,7,3,3","code1":"298588733","code2":null,"time":1013395286000},\
{"ttery":"min","issue":"20130801-3387","code":"0,8,5,2,7","code1":"298818527","code2":null,"time":1013395226000}]'

df = pd.read_json(strtext, orient='records')
df
codecode1code2issuetimettery
8,4,5,2,9297734529NaN20130801-33911013395466000min
7,8,2,1,2298058212NaN20130801-33901013395406000min
5,9,1,2,9298329129NaN20130801-33891013395346000min
3,8,7,3,3298588733NaN20130801-33881013395286000min
0,8,5,2,7298818527NaN20130801-33871013395226000min
df.to_excel('pandas处理json.xlsx',
            index=False,
            columns=["ttery", "issue", "code", "code1", "code2", "time"])

orient参数的五种形式

orient是表明预期的json字符串格式。orient的设置有以下五个值:

1.'split' : dict like {index -> [index], columns -> [columns], data -> [values]}

这种就是有索引,有列字段,和数据矩阵构成的json格式。key名称只能是index,columns和data。

s = '{"index":[1,2,3],"columns":["a","b"],"data":[[1,3],[2,8],[3,9]]}'
df = pd.read_json(s, orient='split')
df
ab
13
28
39

2.'records' : list like [{column -> value}, ... , {column -> value}]

这种就是成员为字典的列表。如我今天要处理的json数据示例所见。构成是列字段为键,值为键值,每一个字典成员就构成了dataframe的一行数据。

strtext = '[{"ttery":"min","issue":"20130801-3391","code":"8,4,5,2,9","code1":"297734529","code2":null,"time":1013395466000},\
{"ttery":"min","issue":"20130801-3390","code":"7,8,2,1,2","code1":"298058212","code2":null,"time":1013395406000}]'

df = pd.read_json(strtext, orient='records')
df
codecode1code2issuetimettery
8,4,5,2,9297734529NaN20130801-33911013395466000min
7,8,2,1,2298058212NaN20130801-33901013395406000min

3.'index' : dict like {index -> {column -> value}}

以索引为key,以列字段构成的字典为键值。如:

s = '{"0":{"a":1,"b":2},"1":{"a":9,"b":11}}'
df = pd.read_json(s, orient='index')
df
ab
12
911

4.'columns' : dict like {column -> {index -> value}}

这种处理的就是以列为键,对应一个值字典的对象。这个字典对象以索引为键,以值为键值构成的json字符串。如下图所示:

s = '{"a":{"0":1,"1":9},"b":{"0":2,"1":11}}'
df = pd.read_json(s, orient='columns')
df
ab
12
911

5.'values' : just the values array。

values这种我们就很常见了。就是一个嵌套的列表。里面的成员也是列表,2层的。

s = '[["a",1],["b",2]]'
df = pd.read_json(s, orient='values')
df
01
a1
b2

❤  pandas读取sql语句

import numpy as np
import pandas as pd
import pymysql


def conn(sql):
    # 连接到mysql数据库
    conn = pymysql.connect(
        host="localhost",
        port=3306,
        user="root",
        passwd="123",
        db="db1",
    )
    try:
        data = pd.read_sql(sql, con=conn)
        return data
    except Exception as e:
        print("SQL is not correct!")
    finally:
        conn.close()


sql = "select * from test1 limit 0, 10"  # sql语句
data = conn(sql)
print(data.columns.tolist())  # 查看字段
print(data)  # 查看数据

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橙子味冰可乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值