python中pandas常用方法

2022-09-04 07:48:11 字數 3065 閱讀 9531

# coding:utf-8

import numpy as np

import pandas as pd

datas = pd.date_range('20140729', periods=6)

# 先建立乙個時間索引,所謂的索引(index)就是每一行資料的id,可以標識每一行的唯一值

print datas

datetimeindex(['2014-07-29', '2014-07-30', '2014-07-31', '2014-08-01',

'2014-08-02', '2014-08-03'],

dtype='datetime64[ns]', freq='d')

# 為了快速入門,我們看一下如何建立乙個6x4的資料:randn函式用於建立隨機數,引數表示行數和列數,dates是上一步建立的索引列

df = pd.dataframe(np.random.randn(6, 4), index=datas, columns=list('abcd'))

print df

a            b                c           d

2014-07-29 0.345746 -0.876197 0.032886 0.986082

2014-07-30 1.922237 0.319706 0.021631 -1.236869

2014-07-31 0.011980 0.376963 -0.149351 -0.172322

2014-08-01 0.909287 2.186534 0.030290 -1.576527

2014-08-02 0.555227 1.123179 -1.687594 -0.957927

2014-08-03 0.232108 1.410862 -0.157567 -0.915288

# 我們還可以使用字典來建立資料框,例如建立乙個列名為a的資料框,索引是自動建立的整數

df2 = pd.dataframe()

print df2

a0 0.303553

1 -0.877653

2 0.239478

3 -0.179406

4 0.766085

# 這又是乙個字典建立dataframe的例子

df2 = pd.dataframe()

print df2

a        b

0 2014-07-29 1

# 假如字典內的資料長度不同,以最長的資料為準,比如b列有4行:

df2 = pd.dataframe()

print df2

a       b

0 2014-07-29 1

1 2014-07-29 1

2 2014-07-29 1

3 2014-07-29 1

# 可以使用dtypes來檢視各行的資料格式

print df2.dtypes

a datetime64[ns]

b int64

dtype: object

# 接著看一下如何檢視資料框中的資料,看一下所有的資料

print df

a            b                c           d

2014-07-29 0.345746 -0.876197 0.032886 0.986082

2014-07-30 1.922237 0.319706 0.021631 -1.236869

2014-07-31 0.011980 0.376963 -0.149351 -0.172322

2014-08-01 0.909287 2.186534 0.030290 -1.576527

2014-08-02 0.555227 1.123179 -1.687594 -0.957927

2014-08-03 0.232108 1.410862 -0.157567 -0.915288

# 使用head檢視前幾行資料(預設是前5行),不過你可以指定前幾行

print df.head()

# 檢視前三行資料

print df.head(3)

# 使用tail檢視後2行資料

print df.tail(2)

# 檢視資料框的索引

print df.index

# 檢視列名用columns

print df.columns

index([u'a', u'b', u'c', u'd'], dtype='object')

# 檢視資料值,用values

print df.values

# 檢視描述性統計,用describe

print df.describe()

a              b              c            d

count 6.000000 6.000000 6.000000 6.000000

mean 0.662764 0.756841 -0.318284 -0.645475

std 0.688200 1.059073 0.676719 0.924433

min 0.011980 -0.876197 -1.687594 -1.576527

25% 0.260518 0.334020 -0.155513 -1.167134

50% 0.450486 0.750071 -0.063860 -0.936608

75% 0.820772 1.338941 0.028125 -0.358063

max 1.922237 2.186534 0.032886 0.986082

# 使用type看一下輸出的描述性統計是什麼樣的資料型別——dataframe資料

print type(df.describe())

# 使用t來轉置資料,也就是行列轉換

print df.t

# 對資料進行排序,用到了sort,引數可以指定根據哪一列資料進行排序。

print df.sort(columns='c')

pandas中DataFrame常用內容整理

1.dataframe概述 series的模型是二維陣列.2.dataframe建立 1 通過二維陣列建立 二維陣列建立 dataframe1 pa.dataframe 1,2,3 4,5,6 index jenny danny columns c e m print 通過二維陣列建立 print ...

Python中的Pandas模組

目錄 pandas series 序列的建立 序列的讀取 dataframe dataframe的建立 dataframe資料的讀取 panel panel的建立 pandas python data analysis library 是基於numpy 的一種工具,該工具是為了解決資料分析任務而建立...

pandas 常用函式

本文翻譯自文章 pandas cheat sheet python for data science 同時新增了部分註解。對於資料科學家,無論是資料分析還是資料探勘來說,pandas是乙個非常重要的python包。它不僅提供了很多方法,使得資料處理非常簡單,同時在資料處理速度上也做了很多優化,使得和...