Pandas中创建DataFrame​​​​​

 1 Pandas中创建DataFrame​​​​​

1.1 pd.DataFrame(ndarray数据,index=[行索引1行索引2],colunms=[列索引1列索引2])


import numpy as np
import pandas as pd
a=pd.DataFrame(np.arange(18).reshape(3,6),index=['a','b','c'],columns=['A','B','C','D','E','F'])
print(a)
#  A   B   C   D   E   F
#  a   0   1   2   3   4   5
#  b   6   7   8   9  10  11
#  c  12  13  14  15  16  17
1.2 pd.DataFrame(dict数据)


a=pd.DataFrame([{'a':0,'b':3,'c':6},{'a':1,'b':4,'c':7},{'a':2,'b':8,'c':5}]) #带字典的列表
b=pd.DataFrame({'a':[0,1,2],'b':[3,4,8],'c':[6,7,5]}) #字典
c=pd.DataFrame(dict(a=[0,1,2],b=[3,4,5],c=[6,7,8])) #字典
#out: a  b  c
#   0  0  3  6
#   1  1  4  7
#   2  2  8  5
2 DataFrame属性

2.1 df.shape :查看DataFrame的形状

2.2 df.dtypes:返回DataFrame的列数据类型

2.3 df[df.index==某行索引值]:对某行内容进行索引

import pandas as pd
A=pd.DataFrame({'a':[1,2,3],'b':[2,3,4],'c':[2,4,5]})
print(A[A.index==0])
#Out:a  b  c
#  0  1  2  2
print(A[A.a==1])
#Out:a  b  c
#  0  1  2  2
2.4 df.columns:列索引

2.5 df.head():仅显示前面几行数据(默认是前五行)

2.6 df.tail():仅显示最后几行数据(默认是后五行)

2.7 dataframe.values:仅返回数据框中的值,轴标签将被删除

2.8 dataframe.astype: 对DataFrame的列数据类型转换

df = pd.DataFrame({'age':    [ 3,  29],
                   'height': [94, 170],
                   'weight': [31, 115]})
#df
   age  height  weight
0    3      94      31
1   29     170     115
#df.dtypes
age       int64
height    int64
weight    int64
dtype: object
#df.values
array([[  3,  94,  31],
       [ 29, 170, 115]])
3 DataFrame的索引
3.1 查看某列——df['列索引']
import pandas as pd
A=pd.DataFrame(dict(a=[1,2,3],b=[2,3,4],c=[23,5,2]))
print(A['a'])
#Out:0    1
#     1    2
#     2    3
#     Name: a, dtype: int64
3.2 查看某个数据——df['列索引']['行索引']

import pandas as pd
import numpy as np
B=pd.DataFrame(np.arange(12).reshape(3,4),index=['a','b','c'],columns=['AA','BB','CC','DD'])
print(B['AA'][0]) 
#Out:0
3.2 查看某行——df.loc[]函数和df.iloc[]函数


import pandas as pd
import numpy as np
a=pd.DataFrame(np.arange(15).reshape(3,5),index=['a','b','c'],columns=['aa','bb','cc','dd','ee'])
print(a)
#Out1:  aa  bb  cc  dd  ee
#    a   0   1   2   3   4
#    b   5   6   7   8   9
#    c  10  11  12  13  14
print(a.loc['a':'b','aa':'dd'])
#Out2: aa  bb  cc  dd
#   a   0   1   2   3
#   b   5   6   7   8
print(a.iloc[0:1,0:3])
#Out3: aa  bb  cc
#   a   0   1   2

3.2.1 df.loc[索引]函数:方框+'索引',闭区间

3.2.2 df.iloc[下标]函数:方框+下标,左到右不到

在读取Series的数据时,就有通过方括号+索引/下标值的方式读取对应数据这两种方式。这是因为Series的索引是可以重新定义的,而下标始终是不变的(0开始)。

3.4 查看行数据时还可以以下方式

df[df.index==某一行索引值]:查看某行数据

df[df.某列索引名==该列的一个值]:对一行或多行内容进行索引

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值