Python 熊猫们在一个小组上循环
我有一个数据集,其中有一个类别字段“City”和两个指标“Age”和“Weight”。我想使用循环为每个城市绘制一个散点图。然而,我正在努力将我需要的GROUPBY和loop组合到一个语句中。如果我只使用for循环,我会为每条记录生成一个图表,如果我按分组,我会得到正确数量的图表,但没有值 下面是我的代码,它只使用for循环,我的组被注释掉了:Python 熊猫们在一个小组上循环,python,pandas,for-loop,matplotlib,pandas-groupby,Python,Pandas,For Loop,Matplotlib,Pandas Groupby,我有一个数据集,其中有一个类别字段“City”和两个指标“Age”和“Weight”。我想使用循环为每个城市绘制一个散点图。然而,我正在努力将我需要的GROUPBY和loop组合到一个语句中。如果我只使用for循环,我会为每条记录生成一个图表,如果我按分组,我会得到正确数量的图表,但没有值 下面是我的代码,它只使用for循环,我的组被注释掉了: import pandas as pd import numpy as np import matplotlib.pylab as plt d =
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="^")
不要多次调用
plt.figure
,因为每次调用都会创建一个新的图形(粗略地说,是窗口)
plt.subplot
返回一个图形,fig
和一个轴,ax
ax=ax
传递给Panda的绘图方法,则所有绘图都将
在同一轴线上有多高要为每个城市制作单独的数字:
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
d = {'City': ['London', 'New York', 'New York', 'London', 'Paris',
'Paris', 'New York', 'New York', 'London', 'Paris'],
'Age': [36., 42., 6., 66., 38., 18., 22., 43., 34., 54],
'Weight': [225, 454, 345, 355, 234, 198, 400, 256, 323, 310]}
df = pd.DataFrame(d)
groups = df.groupby(['City'])
for city, grp in groups: # 1
fig, ax = plt.subplots(figsize=(5, 4))
grp.plot(kind='scatter', x='Age', y='Weight', # 2
ax=ax)
plt.show()
grp
,而不是df
我使用了另一篇文章中的group by,并将其插入到我的代码中,以生成每个group by的图表:
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)
groups = df.groupby(['City'])
for city, grp in groups:
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 == city, color="red", marker="^")
因此,在某些情况下,这将是一个很好的解决方案,但在我的例子中,当我说我想要每个城市的散点图时,我实际上是指每个城市的单独图表/数字。原因是完整的数据集要大得多,因此我需要查看不同图表上的不同点。不确定原因,但上面的两个示例抛出了错误:ValueError:无效图表类型在我运行它们时给出了散点。我已经接受了您建议的组,并将其插入到我的代码中,这样我现在就可以获得我想要的输出了。我想知道为什么我在你的结构中看到无效的图表类型错误。我自己还没弄明白。
kind='scatter'
是。哦,是的,我已经更新了库,现在正在工作。谢谢。不确定这是否需要一个新问题,但第一个示例似乎不再适用于pandas版本0.17.1和matplotlib 1.5.0。取而代之的是,它用折线图而不是散点图生成4个单独的图形。我找不到一个与这些版本一起工作的替代方案来生成具有多个组的单个图形。
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)
groups = df.groupby(['City'])
for city, grp in groups:
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 == city, color="red", marker="^")