Pandas学习(3)DataFream操作2

分组(Grouping),适用于有重复数据可统计、可分组的DataFrame
“group by” 指的是涵盖下列一项或多项步骤的处理流程:
分割:按条件把数据分割成多组;
应用:为每组单独应用函数;
组合:将处理结果组合成一个数据结构。

import numpy as np
import pandas as pd
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
ABCD
0fooone0.433992-1.333293
1barone0.194936-0.391367
2footwo1.1996330.119350
3barthree-0.7868950.729469
4footwo0.4501720.418308
5bartwo-0.256733-0.802991
6fooone0.2023260.758224
7foothree-1.3816620.517538
df1=df.groupby('A').sum()#计算
df1#统计A组中所有bar、foo的和
CD
A
bar-0.848692-0.464888
foo0.9044600.480128

也可进行多列分组:

df.groupby(['A', 'B']).sum()#同时统计A、B列可分组数据和
CD
AB
barone0.194936-0.391367
three-0.7868950.729469
two-0.256733-0.802991
fooone0.636317-0.575069
three-1.3816620.517538
two1.6498050.537658

多层索引与重塑:
有时候为了更好的查看DataFrame数据,我们可以使用多层索引,并会使用到重塑方法。
首先需要创建一个MultiIndex(层次索引)对象
MultiIndex可以从阵列(使用的列表来创建 MultiIndex.from_arrays()),
元组(使用的阵列 MultiIndex.from_tuples()),
一个交叉组iterables(使用的 MultiIndex.from_product()),
或者一个DataFrame(使用 MultiIndex.from_frame())创建。
下列实例使用一个元组列表和Index构造函数创建

tuples = list(zip(*[['bar', 'bar', 'baz', 'baz',
                      'foo', 'foo', 'qux', 'qux'],
                     ['one', 'two', 'one', 'two',
                     'one', 'two', 'one', 'two']]))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])  
df = pd.DataFrame(np.random.randn(8, 4), index=index, columns=['A', 'B','C','D'])
df
ABCD
firstsecond
barone0.271837-0.997848-2.000626-0.034044
two-0.779253-1.503962-1.2441410.337168
bazone1.060027-1.522924-0.4009590.813439
two1.896187-0.5251000.3417340.120456
fooone-0.5491810.4743250.6053490.161852
two-0.7322630.756273-0.9863970.810937
quxone-0.755619-1.4570630.6263150.405198
two0.3772850.494047-1.161364-0.772654

stack()方法把 DataFrame 多列压缩至一层:

df1=df.stack()
df1
first  second   
bar    one     A    0.271837
               B   -0.997848
               C   -2.000626
               D   -0.034044
       two     A   -0.779253
               B   -1.503962
               C   -1.244141
               D    0.337168
baz    one     A    1.060027
               B   -1.522924
               C   -0.400959
               D    0.813439
       two     A    1.896187
               B   -0.525100
               C    0.341734
               D    0.120456
foo    one     A   -0.549181
               B    0.474325
               C    0.605349
               D    0.161852
       two     A   -0.732263
               B    0.756273
               C   -0.986397
               D    0.810937
qux    one     A   -0.755619
               B   -1.457063
               C    0.626315
               D    0.405198
       two     A    0.377285
               B    0.494047
               C   -1.161364
               D   -0.772654
dtype: float64

stack() 的逆操作是 unstack()

df1.unstack()
ABCD
firstsecond
barone0.271837-0.997848-2.000626-0.034044
two-0.779253-1.503962-1.2441410.337168
bazone1.060027-1.522924-0.4009590.813439
two1.896187-0.5251000.3417340.120456
fooone-0.5491810.4743250.6053490.161852
two-0.7322630.756273-0.9863970.810937
quxone-0.755619-1.4570630.6263150.405198
two0.3772850.494047-1.161364-0.772654

数据输入 / 输出
文本文件 读 写
CSV read_csv to_csv
Fixed-WidthTextFile read_fwf
JSON read_json to_json
HTML read_html to_html
Local clipboard read_clipboard to_clipboard
MS Excel read_excel to_excel

二进制文件
OpenDocument read_excel
HDF5 Format read_hdf to_hdf
Feather Format read_feather to_feather
Parquet Format read_parquet to_parquet
ORC Format read_orc
Msgpack read_msgpack to_msgpack
Stata read_stata to_stata
SAS read_sas
SPSS read_spss
Python Pickle Format read_pickle to_pickle

SQL
SQL read_sql to_sql
Google Big Queryread_gbq to_gbq

df
ABCD
firstsecond
barone0.271837-0.997848-2.000626-0.034044
two-0.779253-1.503962-1.2441410.337168
bazone1.060027-1.522924-0.4009590.813439
two1.896187-0.5251000.3417340.120456
fooone-0.5491810.4743250.6053490.161852
two-0.7322630.756273-0.9863970.810937
quxone-0.755619-1.4570630.6263150.405198
two0.3772850.494047-1.161364-0.772654
#将df写入csv文件。
df.to_csv('file.csv')
#打开创建好的csv文件夹
pd.read_csv('file.csv')
firstsecondABCD
0barone0.271837-0.997848-2.000626-0.034044
1bartwo-0.779253-1.503962-1.2441410.337168
2bazone1.060027-1.522924-0.4009590.813439
3baztwo1.896187-0.5251000.3417340.120456
4fooone-0.5491810.4743250.6053490.161852
5footwo-0.7322630.756273-0.9863970.810937
6quxone-0.755619-1.4570630.6263150.405198
7quxtwo0.3772850.494047-1.161364-0.772654

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值