Python max関数を使ってDataFrameの列の最大値を求める

環境
Python 3.9.13
Windows 10 Home 64bit
PyCharm 2022.2.1 (Community Edition)

書式
import pandas as pd
DataFrame['列名’].max()
maxを使用してDataFrameの列の最大値を求めます。

pandasをインストールします。
pip install pandas
pip install numpy

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import numpy as np
import pandas as pd
df = pd.DataFrame(
np.random.randint(1,10,size=(4, 3)),#3行 4列のDataFrameの列
columns=list('123'))
print(df)
tt = df['1'].max()
print ("列1 の最大値:",tt)
tt = df['2'].max()
print ("列2 の最大値:",tt)
tt = df['3'].max()
print ("列3 の最大値:",tt)
import numpy as np import pandas as pd df = pd.DataFrame( np.random.randint(1,10,size=(4, 3)),#3行 4列のDataFrameの列 columns=list('123')) print(df) tt = df['1'].max() print ("列1 の最大値:",tt) tt = df['2'].max() print ("列2 の最大値:",tt) tt = df['3'].max() print ("列3 の最大値:",tt)
import numpy as np
import pandas as pd

df = pd.DataFrame(
    np.random.randint(1,10,size=(4, 3)),#3行 4列のDataFrameの列
    columns=list('123'))

print(df)

tt = df['1'].max()
print ("列1 の最大値:",tt)

tt = df['2'].max()
print ("列2 の最大値:",tt)

tt = df['3'].max()
print ("列3 の最大値:",tt)

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1 2 3
0 9 6 6
1 6 3 9
2 7 4 6
3 5 2 3
1 の最大値: 9
2 の最大値: 6
3 の最大値: 9
1 2 3 0 9 6 6 1 6 3 9 2 7 4 6 3 5 2 3 列1 の最大値: 9 列2 の最大値: 6 列3 の最大値: 9
   1  2  3
0  9  6  6
1  6  3  9
2  7  4  6
3  5  2  3
列1 の最大値: 9
列2 の最大値: 6
列3 の最大値: 9

 

Python

Posted by arkgame