Pandas数据规整

简介: Pandas数据规整数据分析和建模方面的大量编程工作都是用在数据准备上的,有时候存放在文件或数据库中的数据并不能满足数据处理应用的要求Pandas提供了一组高级的、灵活的、高效的核心函数和算法,它们能够轻松地将数据规...

Pandas数据规整

数据分析和建模方面的大量编程工作都是用在数据准备上的,有时候存放在文件或数据库中的数据并不能满足数据处理应用的要求

Pandas提供了一组高级的、灵活的、高效的核心函数和算法,它们能够轻松地将数据规整化为你需要的形式


合并

连接

Pandas提供了大量方法,能轻松的对Series,DataFrame和Panel执行合并操作

连接pandas对象 .concat()

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.randn(10, 4))
df.head()
0 1 2 3
0 0.231308 1.193636 -0.033288 0.826399
1 -0.421474 -0.618510 -1.266325 -0.439435
2 -0.279457 0.578144 1.131353 -0.639720
3 -1.197750 -0.446579 0.495728 0.900704
4 -0.638926 -0.233019 -1.106248 -0.762133
pieces = [df[:2], df[3:5], df[7:]] # 这里面切片是前闭后开的
pieces
[          0         1         2         3
 0  0.231308  1.193636 -0.033288  0.826399
 1 -0.421474 -0.618510 -1.266325 -0.439435,
           0         1         2         3
 3 -1.197750 -0.446579  0.495728  0.900704
 4 -0.638926 -0.233019 -1.106248 -0.762133,
           0         1         2         3
 7 -0.265515 -0.705797  0.695531 -0.257374
 8  0.552615 -0.137180  0.859215 -0.853752
 9 -1.014105  0.392409 -1.832748  0.612679]
df2 = pd.concat(pieces)
df2
0 1 2 3
0 0.231308 1.193636 -0.033288 0.826399
1 -0.421474 -0.618510 -1.266325 -0.439435
3 -1.197750 -0.446579 0.495728 0.900704
4 -0.638926 -0.233019 -1.106248 -0.762133
7 -0.265515 -0.705797 0.695531 -0.257374
8 0.552615 -0.137180 0.859215 -0.853752
9 -1.014105 0.392409 -1.832748 0.612679

追加 .append()

df = pd.DataFrame(np.random.randn(4, 4), columns=['A','B','C','D'])
df
A B C D
0 1.295901 -0.742636 0.873728 -0.810075
1 1.073456 0.344627 0.156597 1.460616
2 1.696282 -1.272457 1.226460 -1.944458
3 -0.473047 0.147528 -0.538231 0.125467
s = df.iloc[2]
s
A    1.696282
B   -1.272457
C    1.226460
D   -1.944458
Name: 2, dtype: float64
df.append(s, ignore_index=True)
A B C D
0 1.295901 -0.742636 0.873728 -0.810075
1 1.073456 0.344627 0.156597 1.460616
2 1.696282 -1.272457 1.226460 -1.944458
3 -0.473047 0.147528 -0.538231 0.125467
4 1.696282 -1.272457 1.226460 -1.944458

分组

group by():一般指以下一个或多个操作步骤

  • Splitting 将数据分组
  • Applying 对每个分组应用不同的function
  • Combining 使用某种数据结果展示结果
df = pd.DataFrame({
    'A' : ['foo', 'bar', 'foo', 'bar','foo', 'bar', 'foo', 'foo'],
    'B' : ['one', 'one', 'two', 'three','two', 'two', 'one', 'three'],
    'C' : np.random.randn(8),
    'D' : np.random.randn(8)
    })
df
A B C D
0 foo one 0.556699 1.543716
1 bar one -0.905349 -0.054870
2 foo two 1.220397 -0.589706
3 bar three 0.637305 -0.046351
4 foo two -0.150553 -0.889157
5 bar two -0.771132 0.196547
6 foo one 0.008275 -0.571672
7 foo three 0.228275 -1.164593
# 分组后sum求和:
a = df.groupby('A').sum()
a
C D
A
bar -1.039176 0.095325
foo 1.863094 -1.671411
a = df.groupby('A',as_index=False).sum()
a
A C D
0 bar -1.039176 0.095325
1 foo 1.863094 -1.671411
# 对多列分组后sum:
b = df.groupby(['A','B']).sum()
b
C D
A B
bar one -0.905349 -0.054870
three 0.637305 -0.046351
two -0.771132 0.196547
foo one 0.564975 0.972044
three 0.228275 -1.164593
two 1.069844 -1.478862
b = df.groupby(['A','B'],as_index=False).sum()
b
A B C D
0 bar one -0.905349 -0.054870
1 bar three 0.637305 -0.046351
2 bar two -0.771132 0.196547
3 foo one 0.564975 0.972044
4 foo three 0.228275 -1.164593
5 foo two 1.069844 -1.478862
目录
相关文章
|
21天前
|
数据处理 Python
如何使用Python的Pandas库进行数据排序和排名
【4月更文挑战第22天】Pandas Python库提供数据排序和排名功能。使用`sort_values()`按列进行升序或降序排序,如`df.sort_values(by='A', ascending=False)`。`rank()`函数用于计算排名,如`df['A'].rank(ascending=False)`。多列操作可传入列名列表,如`df.sort_values(by=['A', 'B'], ascending=[True, False])`和分别对'A'、'B'列排名。
24 2
|
21天前
|
存储 Python
使用Pandas库对非数值型数据进行排序和排名
在Pandas中,支持对非数值型数据排序和排名。可按以下方法操作:1) 字符串排序,使用`sort_values()`,如`sorted_df = df.sort_values(by='Name', ascending=False)`进行降序排序;2) 日期排序,先用`to_datetime()`转换,再排序,如`sorted_df = df.sort_values(by='Date')`;3) 自定义排序,结合`argsort()`和自定义规则。
27 2
|
2月前
|
数据格式 Python
如何使用Python的Pandas库进行数据透视图(melt/cast)操作?
Pandas的`melt()`和`pivot()`函数用于数据透视。基本步骤:导入pandas,创建DataFrame,然后使用这两个函数转换数据格式。示例代码展示了如何通过`melt()`转为长格式,再用`pivot()`恢复为宽格式。输入数据是包含'Name'和'Age'列的DataFrame,最终结果经过转换后呈现出不同的布局。
44 6
|
2月前
|
数据处理 Python
如何使用Python的Pandas库进行数据排序和排名?
Pandas在Python中提供数据排序和排名功能。使用`sort_values()`进行排序,如`df.sort_values(by='A', ascending=False)`进行降序排序;用`rank()`进行排名,如`df['A'].rank(ascending=False)`进行降序排名。多列操作可传入列名列表,如`df.sort_values(by=['A', 'B'], ascending=[True, False])`。
32 6
|
2月前
|
索引 Python
如何使用Python的Pandas库进行数据合并和拼接?
【2月更文挑战第28天】【2月更文挑战第103篇】如何使用Python的Pandas库进行数据合并和拼接?
|
2月前
|
索引 Python
如何在Python中,Pandas库实现对数据的时间序列分析?
Pandas在Python中提供强大的时间序列分析功能,包括:1) 使用`pd.date_range()`创建时间序列;2) 通过`pd.DataFrame()`将时间序列转为DataFrame;3) `set_index()`设定时间列作为索引;4) `resample()`实现数据重采样(如按月、季度);5) `rolling()`进行移动窗口计算,如计算移动平均;6) 使用`seasonal_decompose()`进行季节性调整。这些工具适用于各种时间序列分析场景。
44 0
|
17天前
|
数据采集 数据处理 索引
如何使用 Pandas 删除 DataFrame 中的非数字类型数据?
如何使用 Pandas 删除 DataFrame 中的非数字类型数据?
27 3
|
18天前
|
存储 数据挖掘 数据处理
使用pandas高效读取筛选csv数据
本文介绍了使用Python的Pandas库读取和处理CSV文件。首先,确保安装了Pandas,然后通过`pd.read_csv()`函数读取CSV,可自定义分隔符、列名、索引等。使用`head()`查看数据前几行,`info()`获取基本信息。Pandas为数据分析提供强大支持,是数据科学家的常用工具。
23 0
|
20天前
|
数据挖掘 数据处理 索引
如何使用Python的Pandas库进行数据筛选和过滤?
Pandas是Python数据分析的核心库,提供DataFrame数据结构。基本步骤包括导入库、创建DataFrame及进行数据筛选。示例代码展示了如何通过布尔索引、`query()`和`loc[]`方法筛选`Age`大于19的记录。
21 0
|
20天前
|
索引 Python
如何使用Pandas进行数据合并?
Pandas提供`merge()`, `join()`, `concat()`等方法进行数据合并。基本步骤包括导入pandas库、创建或加载DataFrame,然后调用这些方法合并数据。示例中展示了如何使用`merge()`和`join()`:创建两个DataFrame `df1`和`df2`,通过`merge()`基于索引合并,以及`join()`进行外连接合并。
21 0