Python-pandas-basic-functionality
Pythonパンダ-基本機能
ここまでで、3つのPandas DataStructureとそれらの作成方法について学びました。 リアルタイムデータ処理における重要性のため、主にDataFrameオブジェクトに焦点を当て、いくつかの他のDataStructureについても説明します。
シリーズの基本機能
Sr.No. | Attribute or Method & Description |
---|---|
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))
print s
その*出力*は次のとおりです-
0 0.967853
1 -0.148368
2 -1.395906
3 -1.758394
dtype: float64
axes
シリーズのラベルのリストを返します。
import pandas as pd
import numpy as np
#Create a series with 100 random numbers
s = pd.Series(np.random.randn(4))
print ("The axes are:")
print s.axes
その*出力*は次のとおりです-
The axes are:
[RangeIndex(start=0, stop=4, step=1)]
上記の結果は、0から5までの値のリストのコンパクトな形式、つまり[0,1,2,3,4]です。
空の
オブジェクトが空かどうかを示すブール値を返します。 Trueは、オブジェクトが空であることを示します。
import pandas as pd
import numpy as np
#Create a series with 100 random numbers
s = pd.Series(np.random.randn(4))
print ("Is the Object empty?")
print s.empty
その*出力*は次のとおりです-
Is the Object empty?
False
ndim
オブジェクトの次元数を返します。 定義により、シリーズは1Dデータ構造であるため、
import pandas as pd
import numpy as np
#Create a series with 4 random numbers
s = pd.Series(np.random.randn(4))
print s
print ("The dimensions of the object:")
print s.ndim
その*出力*は次のとおりです-
0 0.175898
1 0.166197
2 -0.609712
3 -1.377000
dtype: float64
The dimensions of the object:
1
size
シリーズのサイズ(長さ)を返します。
import pandas as pd
import numpy as np
#Create a series with 4 random numbers
s = pd.Series(np.random.randn(2))
print s
print ("The size of the object:")
print s.size
その*出力*は次のとおりです-
0 3.078058
1 -1.207803
dtype: float64
The size of the object:
2
値
シリーズの実際のデータを配列として返します。
import pandas as pd
import numpy as np
#Create a series with 4 random numbers
s = pd.Series(np.random.randn(4))
print s
print ("The actual data series is:")
print s.values
その*出力*は次のとおりです-
0 1.787373
1 -0.605159
2 0.180477
3 -0.140922
dtype: float64
The actual data series is:
[ 1.78737302 -0.60515881 0.18047664 -0.1409218 ]
頭と尾
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))
print ("The original series is:")
print s
print ("The first two rows of the data series:")
print s.head(2)
その*出力*は次のとおりです-
The original series is:
0 0.720876
1 -0.765898
2 0.479221
3 -0.139547
dtype: float64
The first two rows of the data series:
0 0.720876
1 -0.765898
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))
print ("The original series is:")
print s
print ("The last two rows of the data series:")
print s.tail(2)
その*出力*は次のとおりです-
The original series is:
0 -0.655091
1 -0.881407
2 -0.608592
3 -2.341413
dtype: float64
The last two rows of the data series:
2 -0.608592
3 -2.341413
dtype: float64
DataFrameの基本機能
ここで、DataFrameの基本機能について理解しましょう。 次の表に、DataFrameの基本機能に役立つ重要な属性またはメソッドを示します。
Sr.No. | Attribute or Method & Description |
---|---|
1 |
T 行と列を入れ替えます。 |
2 |
axes 行軸ラベルと列軸ラベルを唯一のメンバーとして含むリストを返します。 |
3 |
dtypes このオブジェクトのdtypeを返します。 |
4 |
empty NDFrameが完全に空の場合[アイテムなし]の場合はtrue。軸の長さが0の場合。 |
5 |
ndim 軸の数/配列の次元。 |
6 |
shape DataFrameの次元を表すタプルを返します。 |
7 |
size NDFrameの要素の数。 |
8 |
values NDFrameのナンピー表現。 |
9 |
head() 最初のn行を返します。 |
10 |
tail() 最後のn行を返します。 |
DataFrameを作成し、上記の属性がどのように機能するかをすべて見てみましょう。
例
import pandas as pd
import numpy as np
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','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)
print ("Our data series is:")
print df
その*出力*は次のとおりです-
Our data series is:
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 Smith 4.60
6 23 Jack 3.80
T(転置)
DataFrameの転置を返します。 行と列が入れ替わります。
import pandas as pd
import numpy as np
# Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','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)
print ("The transpose of the data series is:")
print df.T
その*出力*は次のとおりです-
The transpose of the data series is:
0 1 2 3 4 5 6
Age 25 26 25 23 30 29 23
Name Tom James Ricky Vin Steve Smith Jack
Rating 4.23 3.24 3.98 2.56 3.2 4.6 3.8
axes
行軸ラベルと列軸ラベルのリストを返します。
import pandas as pd
import numpy as np
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','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)
print ("Row axis labels and column axis labels are:")
print df.axes
その*出力*は次のとおりです-
Row axis labels and column axis labels are:
[RangeIndex(start=0, stop=7, step=1), Index([u'Age', u'Name', u'Rating'],
dtype='object')]
dtypes
各列のデータ型を返します。
import pandas as pd
import numpy as np
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','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)
print ("The data types of each column are:")
print df.dtypes
その*出力*は次のとおりです-
The data types of each column are:
Age int64
Name object
Rating float64
dtype: object
空の
オブジェクトが空かどうかを示すブール値を返します。 Trueは、オブジェクトが空であることを示します。
import pandas as pd
import numpy as np
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','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)
print ("Is the object empty?")
print df.empty
その*出力*は次のとおりです-
Is the object empty?
False
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','Smith','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)
print ("Our object is:")
print df
print ("The dimension of the object is:")
print df.ndim
その*出力*は次のとおりです-
Our object is:
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 Smith 4.60
6 23 Jack 3.80
The dimension of the object is:
2
形状
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','Smith','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)
print ("Our object is:")
print df
print ("The shape of the object is:")
print df.shape
その*出力*は次のとおりです-
Our object is:
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 Smith 4.60
6 23 Jack 3.80
The shape of the object is:
(7, 3)
size
DataFrameの要素の数を返します。
import pandas as pd
import numpy as np
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','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)
print ("Our object is:")
print df
print ("The total number of elements in our object is:")
print df.size
その*出力*は次のとおりです-
Our object is:
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 Smith 4.60
6 23 Jack 3.80
The total number of elements in our object is:
21
値
DataFrameの実際のデータを* NDarray。*として返します
import pandas as pd
import numpy as np
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','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)
print ("Our object is:")
print df
print ("The actual data in our data frame is:")
print df.values
その*出力*は次のとおりです-
Our object is:
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 Smith 4.60
6 23 Jack 3.80
The actual data in our data frame is:
[[Tom' 4.23]
[26 'James' 3.24]
[25 'Ricky' 3.98]
[23 'Vin' 2.56]
[30 'Steve' 3.2]
[29 'Smith' 4.6]
[23 'Jack' 3.8]]
頭と尾
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','Smith','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)
print ("Our data frame is:")
print df
print ("The first two rows of the data frame is:")
print df.head(2)
その*出力*は次のとおりです-
Our data frame is:
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 Smith 4.60
6 23 Jack 3.80
The first two rows of the data frame is:
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','Smith','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)
print ("Our data frame is:")
print df
print ("The last two rows of the data frame is:")
print df.tail(2)
その*出力*は次のとおりです-
Our data frame is:
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 Smith 4.60
6 23 Jack 3.80
The last two rows of the data frame is:
Age Name Rating
5 29 Smith 4.6
6 23 Jack 3.8