Pandas简单使用Series和DataFrame

Series的创建方式
  • 通过标量来创建Series

    x=22
    s1=pd.Series(x)
    s1
    
    0	22
    dype:int64
    
    s2=pd.Series(x,index=list(range(3)))
    s2
    
    0	22
    1	22
    2	22
    dtype:int64
    
  • 通过可迭代对象创建Series

    x=range(1,7)
    s1=pd.Series(x)
    s1
    
    0	1
    1	2
    2	3
    3	4
    4	5
    5	6
    dtype:int64
    
  • 通过列表创建Series

    x=[1,2,3,4,5]
    s1 = pd.Series(x)
    s1
    0	1
    1	2
    2	3
    3	4
    4	5
    
  • 通过字典创建Series

    x=dict(a=22,b=18,c=36)
    x
    {"a":22,"b":18,"c":36}
    
    s1=pd.Series(x)
    s1
    a	22
    b	18
    c	36
    
Series的6个常用属性
  • ndim:返回Series的维数

  • shape:返回Series的行列数

  • size:返回Series元素的个数

  • dtype:返回Series元素的数据类型

  • index:返回Series的索引,得到的只是一个索引对象,我们可以利用list()函数将他们转换为索引列表

  • values:返回Series的数值

    x = [1,3,5,7,9]
    s = pd.Series(x)
    s
    	
    0	1
    1	3
    2	5	
    3	7
    4	9
    dtype: int64
    
    s.ndim
    1
    
    s.shape
    (5,)
    
    s.size
    5
    
    s.dtypes
    dtype("int64")
    
    list(s.index)
    [0,1,2,3,4]
    
    s.values
    array([1,3,5,7,9],dtype=int64)
    
DataFrame的4种创建方式(最外层为字典则是竖着,最外层为列表则是横着)
  • 通过列表组成的列表创建DateFrame

    x=[
    	[1,2,3,4],
    	[5,6,7,8],
    	[9,10,11,12]
    ]
    df1=pd.DataFrame(x)
    df1
    
    	0	1	2	3
    0	1	2	3	4
    1	5	6	7	8
    2	9	10	11	12
    
    df2=pd.DataFrame(x,index=['aa','bb','cc'],columns=list('abcd'))
    df2
    
    	a	b	c	d
    aa	1	2	3	4
    bb	5	6	7	8
    cc	9	10	11	12
    
  • 通过列表组成的字典创建DataFrame

    x = {
    	"name":['张三','李四','王五'],
    	"age":[18,22,20],
    	"sex":["男","女","男"]
    }
    df=pd.DataFrame(x)
    df
    
    	name age sex
    0	张三	18	男 
    1	李四	22	女
    2	王五	20	男
    
  • 通过字典组成的列表创建DataFrame

    x=[
    	{'one':1,'two':2,'three':3},
    	{'one':11,'two':22,'three':33}
    ]
    df=pd.DataFrame(x)
    df
    
    	one	two	three
    0	1	2	3
    1	11	22	33
    
  • 通过字典组成的字典创建DataFrame

    x = {
    	"张三":{'mysql':88,'python':77,'hive':66},
    	"李四":{'mysql':11,'python':22,'hive':33}
    }
    df=pd.DataFrame(x)
    df
    
    		张三	李四
    mysql	88	  11
    python	77	  22
    hive	66	  33
    
DataFrame的7个常用属性
  • ndim:返回DataFrame的维数

  • shape:返回DataFrame的行列数

  • size:返回DataFrame元素的个数

  • dtypes:返回DataFrame每一列元素的数据类型

  • index:返回DataFrame的行索引

  • columns:返回DataFrame的列索引

  • values:返回DataFrame的数值

    x = {
    	"name":["张三","李四","王五"],
    	"age":[18,20,19],
    	"sex":["男","女","男"]
    }
    df=pd.DataFrame(x)
    df
    
    	name  age  sex
    0	张三	 18	  男
    1	李四	 20	  女
    2	王五	 19	  男
    
    df.ndim
    2
    
    df.shape
    (3,3)		##(列,行)
    
    df.size
    9			##一共有9个元素
    
    df.dtypes
    name	object
    age		int64
    sex		object
    
    list(df.index)
    [0,1,2]
    
    list(df.columns)
    ['name','age','sex']
    
    df.values						###numpy的二维数组
    array([
    		['张三','李四','王五'],
    		[18,20,19],
    		["男","女","男"]
    	],dtype=object)
    
    df.values.tolist()
    [['张三', 18, '男'], ['李四', 20, '女'], ['王五', 19, '男']]
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值