このPythonチュートリアルでは、pandasのDataFrameやSeriesオブジェクトのheadとtailを取得するための様々なメソッドについて説明する予定です。
それでは、始めましょう。
なぜpandasのDataFrameやSeriesの先頭と最後尾を取得するのですか?
Pandasはデータ分析に広く使われているPythonの必須ライブラリであることは周知の通りです。
そして、データ分析が非常に大きなデータセットを扱うことはよく知られた事実です。
したがって、大規模なサンプルデータセット(pandas DataFrameやSeriesオブジェクトの形で読み込まれます)の概要を知るためには、pandas DataFrameやSeriesの先頭と最後尾が必要です。
pandas DataFrameクラスの DataFrame.head() と DataFrame.tail() 関数を使用して、pandas DataFrameやSeriesの最初の行と最後の行(デフォルトではこのN = 5)をそれぞれ取得するのが一般的です。
pandas DataFrame のヘッドとテール
では、pandas DataFrameオブジェクトのheadとtailの議論を進める前に、サンプルのpandas DataFrameオブジェクトを作成してみましょう。
サンプルpandas DataFrameオブジェクトの作成
# Import pandas Python moduleimport pandas as pd
# Create a large pandas DataFrame objectdf = pd.DataFrame({'RegNo': [111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125],
'Dept': ['ECE', 'ICE', 'IT', 'CSE', 'CHE', 'EE', 'ME', 'CSE', 'ICE', 'TT', 'ECE', 'IT', 'ME', 'BT', 'EE']})
# Print the created pandas DataFrameprint('Sample pandas DataFrame:)
print(df)
|
結果を出力すると、以下の様になります。
Sample pandas DataFrame: RegNo Dept
0 111 ECE1 112 ICE2 113 IT3 114 CSE4 115 CHE5 116 EE6 117 ME7 118 CSE8 119 ICE9 120 TT10 121 ECE11 122 IT12 123 ME13 124 BT14 125 EE |
pandasのDataFrameのheadを取得する: pandas.DataFrame.head()
# Get the head of the sample pandas Seriesprint('First 10 rows of the sample pandas DataFrame:)
temp_df = df.head(10)
print(temp_df)
|
結果は以下の通りです。
First 10 rows of the sample pandas DataFrame: RegNo Dept
0 111 ECE1 112 ICE2 113 IT3 114 CSE4 115 CHE5 116 EE6 117 ME7 118 CSE8 119 ICE9 120 TT |
pandasのDataFrameの末尾を取得する: pandas.DataFrame.tail()
# Get the tail of the sample pandas Seriesprint('Last 10 rows of the sample pandas DataFrame:)
temp_df = df.tail(10)
print(temp_df)
|
結果は以下の通りです。
Last 10 rows of the sample pandas DataFrame: RegNo Dept
5 116 EE6 117 ME7 118 CSE8 119 ICE9 120 TT10 121 ECE11 122 IT12 123 ME13 124 BT14 125 EE |
pandasのDataFrameの先頭と最後尾を一緒に取得する: pandas.option_context()
# Get the head and tail of the sample pandas DataFrame# Using the pd.option_context() function in Pandasprint('First and Last 5 rows of the sample pandas DataFrame:)
with pd.option_context('display.max_rows',10):
print(df)
|
結果は、以下の通りになります。
First and Last 5 rows of the sample pandas DataFrame: RegNo Dept
0 111 ECE1 112 ICE2 113 IT3 114 CSE4 115 CHE.. ... ...10 121 ECE11 122 IT12 123 ME13 124 BT14 125 EE[15 rows x 2 columns] |
パンダの頭と尻尾 シリーズ
では、pandas Seriesオブジェクトのheadとtailの話を進める前に、サンプルのpandas Seriesオブジェクトを作成してみましょう。
サンプルpandas Seriesオブジェクトの作成
# Import pandas Python moduleimport pandas as pd
# Import NumPy Python moduleimport numpy as np
# Create a pandas Seriessr = pd.Series(np.random.randn(1000))
# Print the created pandas Seriesprint('Sample pandas Series:)
print(sr)
|
結果を出力すると、以下の様になります。
Sample pandas Series:0 -0.1577751 -0.1080952 -0.8765013 -0.5919944 -0.435755 ...
995 1.186127996 -0.898278997 -0.267392998 1.295608999 -2.024564Length: 1000, dtype: float64 |
pandas Seriesのheadを取得:pandas.Series.head()
# Get the head of the sample pandas Seriesprint('First 10 values of the sample pandas Series:)
temp_sr = sr.head(10)
print(temp_sr)
|
結果を出力すると、以下の様になります。
First 10 values of the sample pandas Series:0 -0.1577751 -0.1080952 -0.8765013 -0.5919944 -0.4357555 -1.2044346 -0.0359777 0.0153458 -0.4531179 -0.695302dtype: float64 |
pandas Seriesの末尾を取得する: pandas.Series.tail()
# Get the tail of the sample pandas Seriesprint('Last 10 values of the sample pandas Series:)
temp_sr = sr.tail(10)
print(temp_sr)
|
結果を出力すると、以下の様になります。
Last 10 values of the sample pandas Series:990 -0.239336991 -1.475996992 -0.162860993 0.405505994 0.458872995 1.186127996 -0.898278997 -0.267392998 1.295608999 -2.024564dtype: float64 |
まとめ
この Python チュートリアルでは、 head() と tail() 関数を用いて、pandas の DataFrame や Series の head と tail を取得する方法を学びました。
また、Pandasの pandas.option_context() 関数を使用して、pandas DataFrameのheadとtailを同時に取得する方法も見てきました。
上記の内容を理解し、pandasの関数を使って大きなpandas DataFrameの概要を把握することにわくわくしていただけたと思います。
お読みいただきありがとうございました。
Pythonプログラミングの学習リソースをもっと見るために私たちにご期待ください。