学习汇总:点这里
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