Python数据分析与展示:DataFrame类型简单操作-9

简介: Python数据分析与展示:DataFrame类型简单操作-9

DataFrame类型

DataFrame类型由共用相同索引的一组列组成


DataFrame是一个表格型的数据类型,每列值类型可以不同

DataFrame既有行索引、也有列索引

index axis=0

axis=1 column

DataFrame常用于表达二维数据,但可以表达多维数据


DataFrame类型可以由如下类型创建:


二维ndarray对象

由一维ndarray、列表、字典、元组或Series构成的字典

Series类型

其他的DataFrame类型

DataFrame是二维带“标签”数组


DataFrame基本操作类似Series,依据行列索引


代码示例

# -*- coding: utf-8 -*-
# @File    : dataframe_demo.py
# @Date    : 2018-05-20
import pandas as pd
import numpy as np
# DataFrame对象
# 从二维ndarray对象创建  自动行、列索引
df = pd.DataFrame(np.arange(10).reshape(2, 5))
print(df)
"""
   0  1  2  3  4
0  0  1  2  3  4
1  5  6  7  8  9
"""
# 从一维ndarray对象字典创建
dt = {
    "one": pd.Series([1, 2, 3], index=["a", "b", "c"]),
    "two": pd.Series([5, 6, 7, 8], index=["a", "b", "c", "d"])
      }
df = pd.DataFrame(dt)
print(dt)
"""
{
    'one':
    a    1
    b    2
    c    3
    dtype: int64, 
    'two': 
    a    5
    b    6
    c    7
    d    8
    dtype: int64
}
"""
# 数据根据行列索引自动补齐
df = pd.DataFrame(dt, index=["a", "b", "c"], columns=["one", "two"])
print(df)
"""
   one  two
a    1    5
b    2    6
c    3    7
"""
# 从列表类型的字典创建
dt = {
    "one": [1, 2, 3, 4],
    "two": [5, 6, 7, 9]
      }
df = pd.DataFrame(dt, index=["a", "b", "c", "d"])
print(df)
"""
   one  two
a    1    5
b    2    6
c    3    7
d    4    9
"""
# 获取行索引
print(df.index)
# Index(['a', 'b', 'c', 'd'], dtype='object')
# 获取列索引
print(df.columns)
# Index(['one', 'two'], dtype='object')
# 获取值
print(df.values)
"""
[[1 5]
 [2 6]
 [3 7]
 [4 9]]
"""
# 获取列
print(df["one"])
"""
a    1
b    2
c    3
d    4
Name: one, dtype: int64
"""
#获取行
print(df.ix["a"])
"""
one    1
two    5
Name: a, dtype: int64
"""
# 获取某单元格的值
print(df["one"]["a"])
# 1
相关文章
|
4天前
|
索引 Python
如何使用Python的Pandas库进行数据透视表(pivot table)操作?
使用Pandas在Python中创建数据透视表的步骤包括:安装Pandas库,导入它,创建或读取数据(如DataFrame),使用`pd.pivot_table()`指定数据框、行索引、列索引和值,计算聚合函数(如平均分),并可打印或保存结果到文件。这允许对数据进行高效汇总和分析。
10 2
|
4天前
|
机器学习/深度学习 数据挖掘 计算机视觉
python数据分析工具SciPy
【4月更文挑战第15天】SciPy是Python的开源库,用于数学、科学和工程计算,基于NumPy扩展了优化、线性代数、积分、插值、特殊函数、信号处理、图像处理和常微分方程求解等功能。它包含优化、线性代数、积分、信号和图像处理等多个模块。通过SciPy,可以方便地执行各种科学计算任务。例如,计算高斯分布的PDF,需要结合NumPy使用。要安装SciPy,可以使用`pip install scipy`命令。这个库极大地丰富了Python在科学计算领域的应用。
9 1
|
5天前
|
数据可视化 数据挖掘 Python
Python中数据分析工具Matplotlib
【4月更文挑战第14天】Matplotlib是Python的数据可视化库,能生成多种图表,如折线图、柱状图等。以下是一个绘制简单折线图的代码示例: ```python import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] plt.figure() plt.plot(x, y) plt.title('简单折线图') plt.xlabel('X轴') plt.ylabel('Y轴') plt.show() ```
9 1
|
5天前
|
数据采集 SQL 数据可视化
Python数据分析工具Pandas
【4月更文挑战第14天】Pandas是Python的数据分析库,提供Series和DataFrame数据结构,用于高效处理标记数据。它支持从多种数据源加载数据,包括CSV、Excel和SQL。功能包括数据清洗(处理缺失值、异常值)、数据操作(切片、过滤、分组)、时间序列分析及与Matplotlib等库集成进行数据可视化。其高性能底层基于NumPy,适合大型数据集处理。通过加载数据、清洗、分析和可视化,Pandas简化了数据分析流程。广泛的学习资源使其成为数据分析初学者的理想选择。
10 1
|
7天前
|
索引 Python
python 格式化、set类型和class类基础知识练习(上)
python 格式化、set类型和class类基础知识练习
23 0
|
8天前
|
Python 数据挖掘 存储
Python 数据分析(PYDA)第三版(七)(4)
Python 数据分析(PYDA)第三版(七)
30 1
|
Python Shell 存储
Python 数据分析(PYDA)第三版(七)(3)
Python 数据分析(PYDA)第三版(七)
44 1
Python 数据分析(PYDA)第三版(七)(3)
|
机器学习/深度学习 数据可视化 Python
Python 数据分析(PYDA)第三版(六)(2)
Python 数据分析(PYDA)第三版(六)
54 0
|
机器学习/深度学习 Python 数据挖掘
Python 数据分析(PYDA)第三版(六)(1)
Python 数据分析(PYDA)第三版(六)
55 0
|
8天前
|
Python 数据格式 XML
Python 数据分析(PYDA)第三版(三)(1)
Python 数据分析(PYDA)第三版(三)
64 0