pandas的API介绍

部署运行你感兴趣的模型镜像

目录

创建Series对象

把Python列表 -> Series对象

把Python列表 -> Series对象, 指定索引.

把Python元组 -> Series对象.

把字典 -> Series对象. 键: 索引, 值: 数据

把Numpy的ndarray对象 -> Series

Series的属性

演示

DataFrame对象的创建 

把字典列表 -> DataFrame对象

  把列表+元组 -> DataFrame对象

把 Numpy的ndarray对象 ->DataFrame对象


创建Series对象

import pandas as pd
import numpy as np

把Python列表 -> Series对象

s1 = pd.Series([1, 2, 3])
print(s1)
print(type(s1))

把Python列表 -> Series对象, 指定索引.

s2 = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
# print(s2)
s2

把Python元组 -> Series对象.

s3 = pd.Series(('张三', '男', 23), index=['name', 'gender', 'age'])
s3

把字典 -> Series对象. 键: 索引, 值: 数据

s4 = pd.Series({'name': '张三', 'gender': '男', 'age': 23})
s4

把Numpy的ndarray对象 -> Series

arr = np.arange(1, 6)
s5 = pd.Series(arr, index=['a', 'b', 'c', 'd', 'e'])
s5

Series的属性

s1 = pd.Series(data=[i for i in range(6)], index=[i for i in 'ABCDEF'])
s1

演示

# 演示Series的常用属性
print(s1.index)     # 获取索引列的值, Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')

print(s1.values)    # 获取数值列.   [0 1 2 3 4 5]

# 根据索引获取数据.
print(s1['C'])

DataFrame对象的创建 

把字典列表 -> DataFrame对象

# 1. 定义数据集, 字典 + 列表的方式. 
data_dict = {
    '日期': ['2024-12-19', '2024-12-20', '2024-12-21'],
    '温度': [8, 5, 7], 
    '湿度': [65, 70, 60]
}
# 2. 把上述的 数据集 -> DataFrame对象.
# df1 = pd.DataFrame(data_dict)       # 默认索引从0开始, 逐个递增. 
# df1 = pd.DataFrame(data_dict, index=['a', 'b', 'c'])
df1 = pd.DataFrame(data=data_dict, index=['a', 'b', 'c'])
df1

  把列表+元组 -> DataFrame对象

data_list = [
    ('2024-12-19', 8, 65), 
    ('2024-12-20', 5, 70),
    ('2024-12-21', 7, 60)
]
# 2. 把上述的数据集 -> DataFrame对象.
# df2 = pd.DataFrame(data=data_list)
# 
# 指定列名 和 索引.
df2 = pd.DataFrame(data=data_list, columns=['日期', '温度', '湿度'], index=['a', 'b', 'c'])
df2

把 Numpy的ndarray对象 ->DataFrame对象

pd.DataFrame(np.arange(9).reshape(3, 3))

您可能感兴趣的与本文相关的镜像

Python3.9

Python3.9

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值