pandas的数据结构--Series,DataFrame

注:

  1. 此文章只是记录本人在Bilibili学习pandas的过程
  2. B站视频:https://www.bilibili.com/video/BV1UJ411A7Fs?p=1
  3. 数据来源:https://github.com/peiss/ant-learn-pandas
  4. 编写代码环境为anaconda+ jupyter

Series和DataFrame

import numpy as np
import pandas as pd

Series--列表初始化

a = pd.Series([1,2,'abc',0.5])  # 用列表初始化
a

0 1
1 2
2 abc
3 0.5
dtype: object

a.index # 序列索引

RangeIndex(start=0, stop=4, step=1)

a.values # 值

array([1, 2, 'abc', 0.5], dtype=object)

## 传入列表初始化并自定义index标签
a_diy = pd.Series([1,2,'abc',0.5],index=['a','b','c','d'])
a_diy

a 1
b 2
c abc
d 0.5
dtype: object

a_diy.index

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

Series--字典初始化

data_dic = {'a':100,'b':30,'c':-1}
a = pd.Series(data_dic)
a

a 100
b 30
c -1
dtype: int64

Series--按索引查询

print(a['a'])  # 一个索引返回的是基本数据类型
print("====*======")
print(type(a['a']))

100
*==
<class 'numpy.int64'>

print(a[['a','b']])
print("====*======")
print(type(a[['a','b']]))

a 100
b 30
dtype: int64
*==
<class 'pandas.core.series.Series'>

DataFrame

DataFrame ---字典初始化

a = pd.DataFrame(
    {
        'a':[10,20,30],
        'b':[100,200,300],
        'c':[-1,-2,-3]
    }
)
a

a b c
0 10 100 -1
1 20 200 -2
2 30 300 -3

DataFrame ---查询

  1. 如果查询的是一行或者是一列 返回一个Series
  2. 查询的是多行 返回DataFrame
# 一列
print(a['a'])
print("====*======")
print(type(a['a']))

0 10
1 20
2 30
Name: a, dtype: int64
*==
<class 'pandas.core.series.Series'>

# 多列
print(a[ ['a','b'] ])
print("====*======")
print(type(a[ ['a','b'] ]))

a b
0 10 100
1 20 200
2 30 300
*==
<class 'pandas.core.frame.DataFrame'>

# 一行
print(a.loc[1])
print("====*======")
print(type(a.loc[1]))

a 20
b 200
c -2
Name: 1, dtype: int64
*==
<class 'pandas.core.series.Series'>

# 多行
print(a.loc[0:])
# print(type(a.loc[0,1,2]))

a b c
0 10 100 -1
1 20 200 -2
2 30 300 -3

posted @ 2020-06-11 13:26  Adam_lxd  阅读(169)  评论(0)    收藏  举报