python学习笔记(pandas)

一、series和读取外部数据

numpy能够帮助我们处理数值,但是pandas除了处理数值之外(基于numpy),还能处理其他类型的数据

Series:一维,带标签组

DataFrame:二维,Series容器

创建Series

import string
import numpy as np
import pandas as pd

t=pd.Series(np.arange(10),index=list(string.ascii_uppercase[:10]))
print(t)
print(type(t))
# 字典推导式创建一个字典a
a={string.ascii_uppercase[i]:i for i in range(10)}
# 其中的索引就是字典的键
print(pd.Series(a))
print(pd.Series(a,index=list(string.ascii_uppercase[5:15])))
# 指定其他索引后。若有相对应的则取其值,否则为nan
# pandas会自动根据数据类更改series的dype类型

t_dict={"name":"xiao","age":30,"tel":10086}
b=pd.Series(t_dict)
print(b)
# 通过索引和位置来取都是一样的
print(b[0])
print(b["name"])
print(b[:2])# 取多行
print(b.dtype)

# 运行结果
"""
A    0
B    1
C    2
D    3
E    4
F    5
G    6
H    7
I    8
J    9
dtype: int64
F    5.0
G    6.0
H    7.0
I    8.0
J    9.0
K    NaN
L    NaN
M    NaN
N    NaN
O    NaN
dtype: float64
name     xiao
age        30
tel     10086
dtype: object
xiao
xiao
name    xiao
age       30
dtype: object
object
"""

Series的索引和值

import pandas as pd

t_dict={"name":"xiao","age":30,"tel":10086}
b=pd.Series(t_dict)

print(b.index)
for i in b.index:
    print(i)
print(type(b.index))
print(len(b.index))
print(list(b.index))
print(list(b)[:2])
print(type(b.values))

# 运行结果
"""
name
age
tel
<class 'pandas.core.indexes.base.Index'>
3
['name', 'age', 'tel']
['xiao', 30]
<class 'numpy.ndarray'>
"""

二、dataframe

1、pandas的dataframe的创建

import numpy as np
import pandas as pd
from numpy import reshape

a=pd.DataFrame(np.arange(12).reshape(3,4))
print(a)
b=pd.DataFrame(np.arange(12).reshape(3,4),index=list("abc"),columns=list("xyza"))
print(b)

c={"name":["xiao","zhong"],"age":[17,18],"tel":[2005,2004]}
print(pd.DataFrame(c))
print(type(c))
d=[{"name":"xiao","age":17,"tel":2004},{"name":"zhong","age":17}]# 第二个tel无对应值,则为nan

print(pd.DataFrame(d))

# 运行结果
"""
   0  1   2   3
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11
   x  y   z   a
a  0  1   2   3
b  4  5   6   7
c  8  9  10  11
    name  age   tel
0   xiao   17  2005
1  zhong   18  2004
<class 'dict'>
    name  age     tel
0   xiao   17  2004.0
1  zhong   17     NaN
"""

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值