DataFrame相关常用操作

DataFrame相关常用操作(备忘、随时补充):

series:

Series 是一个一维数组对象 ,类似于 NumPy 的一维 array。它除了包含一组数据还包含一组索引,所以可以把它理解为一组带索引的数组。

创建:

创建一个简单的series实例:

import pandas as pd
a = [1, 2, 3]
myser = pd.Series(a)
print(myser)

运行结果为:

0    1
1    2
2    3
dtype: int64

左侧为索引,右侧为数据,下方显示数据类型int64。当没有显示指定索引的时候,Series 自动以 0 开始,步长为 1 为数据创建索引。

通过字典创建:

import pandas as pd
sites = {1: "Google", 2: "Runoob", 3: "Wiki"}
myser = pd.Series(sites)
print(myser)
print(myser[2])

也可以设置index来选择哪几行被创建进series,设置name来设置名称:

import pandas as pd
sites = {1: "Google", 2: "Runoob", 3: "Wiki"}
myser = pd.Series(sites,index=[1,2],name='hello')
print(myser)
print(myser[2])

'''
1    Google
2    Runoob
Name: hello, dtype: object
Runoob
'''

想要单独获取 Series 对象的索引或者数组内容的时候,可以使用 index 和 values 属性,例如:

import pandas as pd
sites = {1: "Google", 2: "Runoob", 3: "Wiki"}
myser = pd.Series(sites,index=[1,2],name='hello')
print(myser.index)
print(myser.values)

'''
Int64Index([1, 2], dtype='int64')
['Google' 'Runoob']
'''

读取:

根据索引读取内容:

import pandas as pd
a = [1, 2, 3]
myser = pd.Series(a)
print(myser[1])

指定索引名称并通过指定的名称读取:

import pandas as pd
a = [1, 2, 3]
myser = pd.Series(a,index=['x','y','z'])
print(myser)
print(myser['z'])

赋值:

单独赋值:

import pandas as pd
sites = {1: "Google", 2: "Runoob", 3: "Wiki"}
myser = pd.Series(sites,index=[1,2])
print(myser)
myser[2]='baidu'
print(myser) 

'''
1    Google
2    Runoob
dtype: object
1    Google
2     baidu
dtype: object
'''

对Series对象运算:

import pandas as pd
sites = {1: 10, 2: 20, 3: 30}
myser = pd.Series(sites)
print(myser)
print(myser * 2)
print(myser * 2 + 1)
print(myser[myser > 15])

'''
1    10
2    20
3    30
dtype: int64
1    20
2    40
3    60
dtype: int64
1    21
2    41
3    61
dtype: int64
2    20
3    30
dtype: int64
'''

转化为列表list:

import pandas as pd
sites = {1: 10, 2: 20, 3: 30}
myser = pd.Series(sites)
print(myser)
print(my
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值