カテゴリ フィールド「City」と 2 つのメトリック、Age と Weight を持つデータセットがあります。ループを使用して各都市の散布図をプロットしたいと考えています。ただし、必要な group by と loop を 1 つのステートメントで組み合わせるのに苦労しています。for ループを使用するだけでは、レコードごとにグラフが作成され、group by を実行すると、適切な数のグラフが得られますが、値はありません。
これは、コメントアウトされたグループで for ループのみを使用したコードです。
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
d = { 'City': pd.Series(['London','New York', 'New York', 'London', 'Paris',
'Paris','New York', 'New York', 'London','Paris']),
'Age' : pd.Series([36., 42., 6., 66., 38.,18.,22.,43.,34.,54]),
'Weight': pd.Series([225,454,345,355,234,198,400, 256,323,310])
}
df = pd.DataFrame(d)
#for C in df.groupby('City'):
for C in df.City:
fig = plt.figure(figsize=(5, 4))
# Create an Axes object.
ax = fig.add_subplot(1,1,1) # one row, one column, first plot
# Plot the data.
ax.scatter(df.Age,df.Weight, df.City == C, color="red", marker="^")