pandas的學習1 基本介紹

2022-09-15 22:09:18 字數 4761 閱讀 5830

'''

numpy 和 pandas 有什麼不同

如果用 python 的列表和字典來作比較, 那麼可以說 numpy 是列表形式的,沒有數值標籤,而 pandas 就是字典形式。pandas是基於numpy構建的,讓numpy為中心的應用變得更加簡單。

要使用pandas,首先需要了解他主要兩個資料結構:series和dataframe。

'''#todo 可以說 numpy 是列表形式的,沒有數值標籤,而 pandas 就是字典形式!!

import pandas as pd

import numpy as np

s = pd.series([1,3,6,np.nan,44,1])#輸入的引數是乙個列表

#此時s(series)包含了三個部分 1.索引 2.資料 3.資料型別dtype

print(s)

"""0 1.0

1 3.0

2 6.0

3 nan

4 44.0

5 1.0

dtype: float64

"""# dataframe

dates = pd.date_range('20160101',periods=6)

df = pd.dataframe(np.random.randn(6,4),index=dates,columns=['a','b','c','d'])

print(df)

""" a b c d

2016-01-01 -0.253065 -2.071051 -0.640515 0.613663

2016-01-02 -1.147178 1.532470 0.989255 -0.499761

2016-01-03 1.221656 -2.390171 1.862914 0.778070

2016-01-04 1.473877 -0.046419 0.610046 0.204672

2016-01-05 -1.584752 -0.700592 1.487264 -1.778293

2016-01-06 0.633675 -1.414157 -0.277066 -0.442545

"""# dataframe是乙個**型的資料結構,它包含有一組有序的列,每列可以是不同的值型別(數值,字串,布林值等)。dataframe既有行索引也有列索引, 它可以被看做由series組成的大字典。

# 我們可以根據每乙個不同的索引來挑選資料, 比如挑選 b 的元素:

# dataframe 的一些簡單運用

print(df['b'])#挑選b的元素

#此時返回的4各引數 是 1.行索引 2.數值 3.freq(頻率)?? 4.dtype

"""2016-01-01 -2.071051

2016-01-02 1.532470

2016-01-03 -2.390171

2016-01-04 -0.046419

2016-01-05 -0.700592

2016-01-06 -1.414157

freq: d, name: b, dtype: float64

"""# 我們在建立一組沒有給定行標籤和列標籤的資料 df1:

df1 = pd.dataframe(np.arange(12).reshape((3,4)))

print(df1)

#此時沒有給定index和column 預設返回0開始的索引

""" 0 1 2 3

0 0 1 2 3

1 4 5 6 7

2 8 9 10 11

"""df2 = pd.dataframe()

print(df2)

""" a b c d e f

0 1.0 2013-01-02 1.0 3 test foo

1 1.0 2013-01-02 1.0 3 train foo

2 1.0 2013-01-02 1.0 3 test foo

3 1.0 2013-01-02 1.0 3 train foo

"""# 這種方法能對每一列的資料進行特殊對待. 如果想要檢視資料中的型別, 我們可以用 dtype 這個屬性:

# 這個相當於excel的**?

print(df2.dtypes)#使用df2.dtype檢視每一行的資料型別

"""df2.dtypes

a float64

b datetime64[ns]

c float32

d int32

e category

f object

dtype: object

"""print(df2.index)

# 如果想看對列的序號: 相當於行號(行的名稱)

# int64index([0, 1, 2, 3], dtype='int64')

print(df2.columns)#相當於檢視列的名稱

# index(['a', 'b', 'c', 'd', 'e', 'f'], dtype='object')

print(df2.values)#只返回df2的所有值,不返回行號和列號

"""array([[1.0, timestamp('2013-01-02 00:00:00'), 1.0, 3, 'test', 'foo'],

[1.0, timestamp('2013-01-02 00:00:00'), 1.0, 3, 'train', 'foo'],

[1.0, timestamp('2013-01-02 00:00:00'), 1.0, 3, 'test', 'foo'],

[1.0, timestamp('2013-01-02 00:00:00'), 1.0, 3, 'train', 'foo']], dtype=object)

"""# 想知道資料的總結, 可以用 describe():

df2.describe()#使用describe相當於打個總結 返回count mean

""" a c d

count 4.0 4.0 4.0

mean 1.0 1.0 3.0

std 0.0 0.0 0.0

min 1.0 1.0 3.0

25% 1.0 1.0 3.0

50% 1.0 1.0 3.0

75% 1.0 1.0 3.0

max 1.0 1.0 3.0

"""print(df2.t) #轉置資料 反轉資料

#對資料的index(也就是行號)進行排序並且輸出

print(df2.sort_index(axis=1, ascending=false)) #ascending 上公升

""" f e d c b a

0 foo test 3 1.0 2013-01-02 1.0

1 foo train 3 1.0 2013-01-02 1.0

2 foo test 3 1.0 2013-01-02 1.0

3 foo train 3 1.0 2013-01-02 1.0

"""# 對資料 值 排序輸出:

print(df2.sort_values(by='b'))

""" a b c d e f

0 1.0 2013-01-02 1.0 3 test foo

1 1.0 2013-01-02 1.0 3 train foo

2 1.0 2013-01-02 1.0 3 test foo

3 1.0 2013-01-02 1.0 3 train foo

"""

出處:pandas主要的兩個資料結構series,dataframe

可以說numpy是列表形式的,沒有資料標籤,pandas是字典型別的,**形式的dateframe!!

s = pd.series([1,3,6,np.nan,44,1])#輸入的引數是乙個列表   輸出包含了三個部分 1.索引 2.資料 3.資料型別dtype

df = pd.dataframe(np.random.randn(6,4),index=dates,columns=['a','b','c','d'])  相當於生成了乙個**,行是index 列是columns

df2 = pd.dataframe()

這是建立的第二種形式,可以看到是按照每列每列的建造,非常的方便啊!在訓練神經結構的時候對資料處理的時候起到了很大的作用

dateframe.index 返回行的名稱

dateframe.columes 返回列的名稱

dateframe.values 返回的只有值

dateframe.describe 打個總結,在**問題上,對生成的資料進行總結

dateframe.t 轉置翻轉資料

dateframe.sort_index

dateframe.sort_values  都可以起到排序的作用

Pandas 基本介紹

本文是pandas的基本介紹 若用 python 的列表和字典來作比較,那麼可以說 numpy 是列表形式,而 pandas 就是字典形式。pandas是基於numpy構建的,讓numpy為中心的應用變得更加簡單。要使用pandas,首先要了解他主要兩個資料結構 series和dataframe。s...

Pandas介紹安裝 (1)

pandas通常和numpy一起學習,抽時間我會把numpy部分也更新上來。初學者建議按照上面的編號來學習,這樣以來知識點會比較全面,不需要你每個都掌握,只要你每個都是用過,都有一些印象,使用的時候再過來檢視就行了,這才是學習,一定要掌握學習方法。pandas是python裡對資料進行處理和分析比較...

pandas基本操作 1

這裡來介紹一下pandas。由於知識點比較多,決定分幾篇介紹。import numpy as np import pandas as pd pandas是基於numpy的乙個開源python庫。我認為相當於python版excel series是一種一維陣列,是基於ndarray設計的,但是他多了乙...