2021-10-16 Python数据分析学习(一)——初识Series和DataFrame

本文介绍了Python数据分析的基础,包括如何使用Anaconda安装环境,以及如何创建和操作Pandas的Series和DataFrame。首先讲解了Series的创建,通过data和index参数,然后展示了DataFrame的构造,利用data、index和columns属性。最后,演示了Series和DataFrame的组合使用,创建了一个包含姓名和年龄的DataFrame。
摘要由CSDN通过智能技术生成

Python数据分析学习(一)

  1. 安装Anaconda
  2. 打开jupyter notebook

1. Series

1.1 使用data参数创建Series

from Pandas import Series
list1 = ['张三','李四','王五']
params = Series(
    data=list1
)
print(params)

运行结果:

0    张三
1    李四
2    王五
dtype: object

1.2 使用index参数创建自定义索引

list2 = ['第一个人','第二个人','第三个人']
params1 = Series(
    data=list1,
    index=list2
)
print(params1)

运行结果:

第一个人    张三
第二个人    李四
第三个人    王五
dtype: object

1.3 使用字典dict创建Series

dic = {
    '姓名':'张三',
    '年龄':22,
    '性别':'男'
}
params2 = Series(
    data=dic
)
print(params2)

运行结果:

姓名    张三
年龄    22
性别     男
dtype: object

2. DataFrame

2.1 使用data参数创建Dataframe

from pandas import DataFrame
lol_list = [
    ['上单','theShy1',20],
    ['打野','theShy2',21],
    ['中单','theShy3',77],
    ['ADC','theShy4',23],
    ['辅助','theShy5',69]
]
df = DataFrame(
    data=lol_list
)
print(df)

运行结果:

     0        1   2
0   上单  theShy1  20
1   打野  theShy2  21
2   中单  theShy3  77
3  ADC  theShy4  23
4   辅助  theShy5  69

2.2 设定DataFrame中的行列索引值

list1 = ['a','b','c','d','e']
list2 = ['位置','ID号','年龄']
df1 = DataFrame(
    data=lol_list,
    index=list1,
    columns=list2
)
print(df1)

运行结果:

    位置      ID号  年龄
a   上单  theShy1  20
b   打野  theShy2  21
c   中单  theShy3  77
d  ADC  theShy4  23
e   辅助  theShy5  69

2.3 使用字典来创建一个DataFrame数据

dic = {
    '位置':['上单','打野','中单','ADC','辅助'],
    'ID号':['theShy1','theShy2','theShy3','theShy4','theShy5'],
    '年龄':[20,21,77,23,69]
}
df2 = DataFrame(
    data=dic,
    index=list1
)
print(df2)

运行结果:

    位置      ID号  年龄
a   上单  theShy1  20
b   打野  theShy2  21
c   中单  theShy3  77
d  ADC  theShy4  23
e   辅助  theShy5  69

3. Series和DataFrame组合使用

from pandas import Series,DataFrame
list1 = ['张三','李四','王五']
list2 = [11,23,546]
se = Series(list1)
se1 = Series(list2)
print(se)
dic = {
    '姓名':se,
    '年龄':se1
}
df = DataFrame(dic)
print(df)

运行结果:

0    张三
1    李四
2    王五
dtype: object
   姓名   年龄
0  张三   11
1  李四   23
2  王五  546
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值