Python で pandas の DataFrame を使う基本的な方法
Python で pandas を使う基本的な方法をメモしておきます。
DataFrame の基本的な使い方¶
サンプルコード¶
import pandas as pd
data = {
"name": ["Eggplant", "Banana", "Apple", "Daikon", "Cherry", "Avocado"],
"price": [500, 200, 150, 400, 300, 100],
"weight": [100, 400, 500, 200, 300, 550],
}
df = pd.DataFrame(data)
print(df)
実行結果¶
% python sample.py
name price weight
0 Eggplant 500 100
1 Banana 200 400
2 Apple 150 500
3 Daikon 400 200
4 Cherry 300 300
5 Avocado 100 550
DataFrame から一行ずつ、値を取り出す¶
サンプルコード¶
import pandas as pd
data = {
"name": ["Eggplant", "Banana", "Apple", "Daikon", "Cherry", "Avocado"],
"price": [500, 200, 150, 400, 300, 100],
"weight": [100, 400, 500, 200, 300, 550],
}
df = pd.DataFrame(data)
for index, row in df.iterrows():
print(row["name"])
実行結果¶
単一キーでソートして出力する¶
サンプルコード¶
import pandas as pd
data = {
"name": ["Eggplant", "Banana", "Apple", "Daikon", "Cherry", "Avocado"],
"price": [500, 200, 150, 400, 300, 100],
"weight": [100, 400, 500, 200, 300, 550],
}
df = pd.DataFrame(data)
df = df.sort_values("name")
print(df)
実行結果¶
% python sample.py
name price weight
2 Apple 150 500
5 Avocado 100 550
1 Banana 200 400
4 Cherry 300 300
3 Daikon 400 200
0 Eggplant 500 100
複数キーでソートして出力する¶
サンプルコード¶
import pandas as pd
data = {
"name": ["Eggplant", "Banana", "Apple", "Daikon", "Cherry", "Avocado"],
"price": [500, 200, 150, 400, 300, 100],
"weight": [100, 400, 500, 200, 300, 550],
}
df = pd.DataFrame(data)
df = df.sort_values(by=["price", "name"])
print(df)
実行結果¶
% python sample.py
name price weight
5 Avocado 100 550
2 Apple 150 500
1 Banana 200 400
4 Cherry 300 300
3 Daikon 400 200
0 Eggplant 500 100
ソートした値を一行ずつ抽出する¶
サンプルコード¶
import pandas as pd
data = {
"name": ["Eggplant", "Banana", "Apple", "Daikon", "Cherry", "Avocado"],
"price": [500, 200, 150, 400, 300, 100],
"weight": [100, 400, 500, 200, 300, 550],
}
df = pd.DataFrame(data)
df = df.sort_values(by=["price", "name"])
for index, row in df.iterrows():
print(f"{row["name"]}, {row["price"]}, {row["weight"]}")
実行結果¶
% python sample.py
Avocado, 100, 550
Apple, 150, 500
Banana, 200, 400
Cherry, 300, 300
Daikon, 400, 200
Eggplant, 500, 100
空の DataFrame を作成する¶
サンプルコード¶
実行結果¶
DataFrame を結合する¶
サンプルコード¶
import pandas as pd
df1 = pd.DataFrame(
{
"name": ["Eggplant", "Banana", "Apple"],
"price": [500, 200, 150],
"weight": [100, 400, 500],
}
)
df2 = pd.DataFrame(
{
"name": ["Daikon", "Cherry", "Avocado"],
"price": [400, 300, 100],
"weight": [200, 300, 550],
}
)
df = pd.concat([df1, df2], ignore_index=True)
print(df)