Pandas的DataFrame数据类型

纵轴表示不同索引axis=0,横轴表示不同列axis=1

 

 DataFrame类型创建

1.从二维ndarray对象创建

 

 1 import pandas as pd
 2 
 3 import numpy as np
 4 
 5 d=pd.DataFrame(np.arange(10).reshape(2,5))
 6 
 7 d
 8 Out[4]: 
 9    0  1  2  3  4
10 0  0  1  2  3  4
11 1  5  6  7  8  9 #自动生成行索引和列索引

2.从一维ndarray对象字典创建

 1 import pandas as pd
 2 
 3 dt={'one':pd.Series([1,2,3],index=['a','b','c']),
 4 'two':pd.Series([8,7,6,5],index=['a','b','c','w'])}
 5 
 6 dt
 7 Out[9]: 
 8 {'one': a    1
 9  b    2
10  c    3
11  dtype: int64, 'two': a    8
12  b    7
13  c    6
14  w    5
15  dtype: int64}
16 
17 d=pd.DataFrame(dt)#原字典中的键变成列索引值,列索引位值中的Series数据中的索引并集
18 
19 d
20 Out[11]: 
21    one  two
22 a  1.0    8
23 b  2.0    7
24 c  3.0    6
25 w  NaN    5
26 
27 pd.DataFrame(dt,index=['a','b','c'],columns=['two','three'])
28 Out[13]: 
29    two three
30 a    8   NaN
31 b    7   NaN
32 c    6   NaN #缺少的元素会被自动补齐

3.从列表类型的字典创建

 1 import pandas as pd
 2 
 3 dl={'one':[1,2,3,4],'two':[3,5,6,9]}
 4 
 5 pd.DataFrame(dl,index=['a','b','c','d']) #这里注意后加的索引值得跟字典里的值数一样
 6 Out[28]: 
 7    one  two
 8 a    1    3
 9 b    2    5
10 c    3    6
11 d    4    9

 

 

 

 

转载于:https://www.cnblogs.com/ldyj/p/10460364.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值