學習彙總:點這裏
Series基本功能
編號 | 屬性或方法 | 描述 |
---|---|---|
1 | axes | 返回行軸標籤列表。 |
2 | dtype | 返回對象的數據類型(dtype)。 |
3 | empty | 如果系列爲空,則返回True。 |
4 | ndim | 返回底層數據的維數,默認定義:1。 |
5 | size | 返回基礎數據中的元素數。 |
6 | values | 將系列作爲ndarray返回。 |
7 | head() | 返回前n行。 |
8 | tail() | 返回最後n行。 |
>>>import pandas as pd
>>>import numpy as np
#Create a series with 100 random numbers
>>>s = pd.Series(np.random.randn(4))
>>>s
0 0.583227
1 0.441980
2 -0.941337
3 -0.651128
dtype: float64
1.axes:返回系列的標籤列表。
>>>import pandas as pd
>>>import numpy as np
#Create a series with 100 random numbers
>>>s = pd.Series(np.random.randn(4))
>>>s.axes
[RangeIndex(start=0, stop=4, step=1)]
2.empty:返回布爾值,表示對象是否爲空。
>>>import pandas as pd
>>>import numpy as np
#Create a series with 100 random numbers
>>>s = pd.Series(np.random.randn(4))
>>>s.empty
False
3.ndim:v返回對象的維數。
>>>import pandas as pd
>>>import numpy as np
#Create a series with 4 random numbers
>>>s = pd.Series(np.random.randn(4))
>>>s
0 0.202508
1 0.738069
2 -0.138353
3 1.108651
dtype: float64
>>>s.ndim
1
4.size:返回系列的大小(長度)。
>>>import pandas as pd
>>>import numpy as np
#Create a series with 4 random numbers
>>>s = pd.Series(np.random.randn(2))
>>>s
0 0.202508
1 0.738069
2 -0.138353
3 1.108651
dtype: float64
>>>s.size
4
5.values:以數組形式返回系列中的實際數據值。
>>>import pandas as pd
>>>import numpy as np
#Create a series with 4 random numbers
>>>s = pd.Series(np.random.randn(4))
>>>s
0 0.202508
1 0.738069
2 -0.138353
3 1.108651
dtype: float64
>>>s.values
array([ 0.26704634, -1.16349349, 0.28927496, -0.91856357])
6.head()和tail()方法:要查看Series或DataFrame對象的小樣本,請使用head()和tail()方法。
head()返回前n行(觀察索引值)。要顯示的元素的默認數量爲5,但可以傳遞自定義這個數字值。
>>>import pandas as pd
>>>import numpy as np
#Create a series with 4 random numbers
>>>s = pd.Series(np.random.randn(4))
>>>s
0 0.202508
1 0.738069
2 -0.138353
3 1.108651
dtype: float64
>>>s.head(2)
0 -1.403253
1 -0.141997
dtype: float64
tail()返回最後n行(觀察索引值)。 要顯示的元素的默認數量爲5,但可以傳遞自定義數字值。
>>>import pandas as pd
>>>import numpy as np
#Create a series with 4 random numbers
>>>s = pd.Series(np.random.randn(4))
>>>s
0 0.202508
1 0.738069
2 -0.138353
3 1.108651
dtype: float64
>>>s.tail(2)
2 0.159207
3 -0.289111
dtype: float64
DataFrame基本功能
編號 | 屬性或方法 | 描述 |
---|---|---|
1 | T | 轉置行和列。 |
2 | axes | 返回一個列,行軸標籤和列軸標籤作爲唯一的成員。 |
3 | dtypes | 返回此對象中的數據類型(dtypes)。 |
4 | empty | 如果NDFrame完全爲空[無項目],則返回爲True; 如果任何軸的長度爲0。 |
5 | ndim | 軸/數組維度大小。 |
6 | shape | 返回表示DataFrame的維度的元組。 |
7 | size | NDFrame中的元素數。 |
8 | values | NDFrame的Numpy表示。 |
9 | head() | 返回開頭前n行。 |
10 | tail() | 返回最後n行。 |
>>>import pandas as pd
>>>import numpy as np
#Create a Dictionary of series
>>>d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#Create a DataFrame
>>>df = pd.DataFrame(d)
>>>df
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80
1.T(轉置):返回DataFrame的轉置。行和列將交換。
>>>import pandas as pd
>>>import numpy as np
# Create a Dictionary of series
>>>d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
# Create a DataFrame
>>>df = pd.DataFrame(d)
>>>df.T
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80
2.axes:返回行軸標籤和列軸標籤列表。
>>>import pandas as pd
>>>import numpy as np
#Create a Dictionary of series
>>>d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#Create a DataFrame
>>>df = pd.DataFrame(d)
>>>df.axes
[RangeIndex(start=0, stop=7, step=1), Index([u'Age', u'Name', u'Rating'], dtype='object')]
3.dtypes:返回每列的數據類型。
>>>import pandas as pd
>>>import numpy as np
#Create a Dictionary of series
>>>d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#Create a DataFrame
>>>df = pd.DataFrame(d)
>>>df.dtypes
Age int64
Name object
Rating float64
dtype: object
4.empty:返回布爾值,表示對象是否爲空; 返回True表示對象爲空。
>>>import pandas as pd
>>>import numpy as np
#Create a Dictionary of series
>>>d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#Create a DataFrame
>>>df = pd.DataFrame(d)
>>>df.empty
False
5.ndim:返回對象的維數。根據定義,DataFrame是一個2D對象。
>>>import pandas as pd
>>>import numpy as np
#Create a Dictionary of series
>>>d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#Create a DataFrame
>>>df = pd.DataFrame(d)
>>>df
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80
>>>df.ndim
2
6.shape:返回表示DataFrame的維度的元組。 元組(a,b),其中a表示行數,b表示列數。
>>>import pandas as pd
>>>import numpy as np
#Create a Dictionary of series
>>>d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#Create a DataFrame
>>>df = pd.DataFrame(d)
>>>df
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80
>>>df.shape
(7, 3)
7.size:返回DataFrame中的元素數。
>>>import pandas as pd
>>>import numpy as np
#Create a Dictionary of series
>>>d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#Create a DataFrame
>>>df = pd.DataFrame(d)
>>>df
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80
>>>df.size
21
8.values:將DataFrame中的實際數據作爲NDarray返回。
>>>import pandas as pd
>>>import numpy as np
#Create a Dictionary of series
>>>d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#Create a DataFrame
>>>df = pd.DataFrame(d)
>>>df
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80
>>>df.values
array([[25L, 'Tom', 4.23],
[26L, 'James', 3.24],
[25L, 'Ricky', 3.98],
[23L, 'Vin', 2.56],
[30L, 'Steve', 3.2],
[29L, 'Minsu', 4.6],
[23L, 'Jack', 3.8]], dtype=object)
9.head()和tail():要查看DataFrame對象的小樣本,可使用head()和tail()方法。
head()返回前n行(觀察索引值)。顯示元素的默認數量爲5,但可以傳遞自定義數字值。
>>>import pandas as pd
>>>import numpy as np
#Create a Dictionary of series
>>>d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#Create a DataFrame
df = pd.DataFrame(d)
>>>df
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80
>>>df.head(2)
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
tail()返回最後n行(觀察索引值)。顯示元素的默認數量爲5,但可以傳遞自定義數字值。
>>>import pandas as pd
>>>import numpy as np
#Create a Dictionary of series
>>>d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#Create a DataFrame
>>>df = pd.DataFrame(d)
>>>df
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80
>>>df.tail(2)
Age Name Rating
5 29 Minsu 4.6
6 23 Jack 3.8