python进阶-10.pandas基本数据类型(1)

import pandas as pd
import numpy as np
from pandas import Series,DataFrame

文章目录

      • 1,Series
      • 自定义索引
      • 读取 Series
      • Sereis.loc['自定义的索引'] || Sereis.iloc[0-n的数字]
      • xxx.loc['yy']=new 修改和增加
      • 读取多个值,会得到一个新对象
      • 通过字典数据得到Series
      • 2.DataFrame
        • 2.1 把numpy 的2位数组转为DataFrame
      • 2.2通过 字典 来创建DataFrame
      • 读取 DataFrame
      • 增加新列
        • 增加行 与 修改行
      • 读取单一值 df.loc[行,列]
      • 当 我们想增加 新列 ,我们如果直接给数字 或者 是 数组的时候,行索引是自动匹配的
        • 删除
      • 删除

1,Series

  • pandas.Series() 返回一个有 index 和 values 属性的数对象
s=pd.Series([4,5,-7,3])
s
0    4
1    5
2   -7
3    3
dtype: int64
s.index
RangeIndex(start=0, stop=4, step=1)
s.values
array([ 4,  5, -7,  3], dtype=int64)

自定义索引

s1=Series([4,7,6,5,1],index=['a','b','c','d','e'],dtype=float)
s1
a    4.0
b    7.0
c    6.0
d    5.0
e    1.0
dtype: float64
s1.index
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')

读取 Series

  • 1,使用 0-n 的索引
  • 2,自定义的索引
  • #数字索引读取
s1[0]
4.0
  • #自定义索引读取
s1['a']
4.0
  • 如果自定义索引是纯数字,就不能用 0 -n 的索引读取了,只能用自定义
s2=Series([4,7,6,5], index=[3,5,7,9],dtype=np.float64)
s2
3    4.0
5    7.0
7    6.0
9    5.0
dtype: float64
s2[3]
4.0

Sereis.loc[‘自定义的索引’] || Sereis.iloc[0-n的数字]

s2.loc[7]
6.0
s2.iloc[2]
6.0

xxx.loc[‘yy’]=new 修改和增加

s1
a    4.0
b    7.0
c    6.0
d    5.0
e    1.0
dtype: float64
s1.loc['e']=1
s1
a    4.0
b    7.0
c    6.0
d    5.0
e    1.0
dtype: float64
 
 

你可能感兴趣的:(python,numpy,索引)