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