Pandas教程--Series

import numpy as np
import pandas as pd

# 从python list创建
series = pd.Series([1, 2, 3])
print(series)
# [1 2 3]

# 从python tuple创建
series = pd.Series((1, 2, 3))
print(series)
# [1 2 3]

# 从python dictionary创建
series = pd.Series({"one": 1, "two": 2, "three": 3})
print(series)
# 0    1
# 1    2
# 2    3
# dtype: int64


# 从numpy数组创建
array = np.array([1, 2, 3])
series = pd.Series(array)
print(series)
# 0    1
# 1    2
# 2    3
# dtype: int32

# 获取索引
print(series.index)
# RangeIndex(start=0, stop=3, step=1)

# 获取值
print(series.values)
# [1 2 3]

# 创建Series时直接指定索引
series = pd.Series([1, 2, 3], index=["one", "two", "three"])
print(series)
# one      1
# two      2
# three    3
# dtype: int64

# 修改索引
series.index = ["index1", "index2", "index3"]
print(series)
# index1    1
# index2    2
# index3    3
# dtype: int64
个人资料
时海
等级:8
文章:272篇
访问:16.0w
排名: 2
上一篇: HashMap源码
下一篇: 1099-找链表中间节点,只遍历一次
标签: series、pd、print、dtype、int64、面试题
隐藏