这个简单的代码按预期绘制折线图:
james_f=names[(names.name=='James') & (names.sex=='F')]
plt.plot(james_f['year'],james_f['births'])
plt.show()
然后我改变条件,只删除其中一个,然后它开始绘制条形图。为什么以及如何强制绘制折线图?
james_f=names[(names.name=='James')]
plt.plot(james_f['year'],james_f['births'])
plt.show()
添加而不是1 == 1规则,没有任何变化(
james_f=names[(names.name=='James') & ( 1 == 1)]
plt.plot(james_f['year'],james_f['births'])
plt.show()
即使这段代码也会绘制条形图:
james_f=names[(names.name=='James') | (names.name=='John') | (names.name=='Robert') ]
plt.plot(james_f['year'],james_f['births'])
james_f['births']
输出(pandas.core.series.Series):
228 46
343 22
538 11
942 9655
944 5927
2312 26
2329 24
2617 9
2938 8769
....
Name: births, dtype: int64
james_f['births'].min()
返回7没有零或NaN值
>>> print(james_f[james_f['births'].isnull()])
Empty DataFrame
Columns: [name, sex, births, year]
Index: []
>>> james_f.head(10)
name sex births year
343 James F 22 1880
944 James M 5927 1880
2329 James F 24 1881
2940 James M 5441 1881
4372 James F 18 1882
4965 James M 5892 1882
6428 James F 25 1883
7118 James M 5223 1883
8488 James F 33 1884
9320 James M 5693 1884
答案 0 :(得分:2)
不过滤性别会产生每年两次观察:一次针对女性,一种针对男性。有名字的男女人数詹姆斯'情节大不相同,使情节显得非常嘈杂。你有(至少)两个选择:
(1)总结这样的男女人数。
james = names[names.name == 'james']
years = []
births = []
for year, subset in james.groupby('year'):
years.append(year)
births.append(subset.births.sum())
plt.plot(years, births)
拥有更多熊猫技能的人可以将其写成一行。
(2)为男性和女性绘制两条不同的线条。
james = names[names.name == 'james']
for sex, subset in james.groupby('sex'):
plt.plot(subset.year, subset.births, label=sex)
plt.legend()