大熊猫csv文件中的负值和条形图条形图

时间:2017-06-22 15:45:48

标签: pandas group-by pandas-groupby

我有一个包含数据的csv文件,我有一个包含正值和负值的列,我需要以一种方式绘制此列的平均值,其中一个用于负值,一个用于正值。 看看我的数据:

timestamp,heure,lat,lon,ampl,type
2006-01-01 00:00:00,13:58:43,33.837,-9.205,10.3,1
2006-01-02 00:00:00,00:07:28,34.5293,-10.2384,17.7,1
2007-02-01 00:00:00,23:01:03,35.0617,-1.435,-17.1,2
2007-02-02 00:00:00,01:14:29,36.5685,0.9043,36.8,1
....
2011-12-31 00:00:00,05:03:51,34.1919,-12.5061,-48.9,1

我正在使用此代码绘制我的数据:

names =["timestamp","heure","lat","lon","ampl","type"]
data = pd.read_csv('flash.txt',names=names, parse_dates=['timestamp'],index_col=['timestamp'], dayfirst=True)
data['ampl'] = data['ampl'].abs()
yearly = data.groupby(data.index.month)['ampl'].count()
ax = yearly.plot(kind='bar')

所以,我需要取消相关列的值,并且有2个而不是1个,我该怎么办?

1 个答案:

答案 0 :(得分:1)

首先按numpy.sign创建新列signdict创建map

然后将新列名添加到groupby,按size汇总,然后重新unstack

data['sign'] = np.sign(data['ampl']).map({1:'+', -1:'-', 0:'0'})
data['ampl'] = data['ampl'].abs()
yearly = data.groupby([data.index.month, 'sign'])['ampl'].size().unstack()
yearly.plot(kind='bar')

What is the difference between size and count in pandas?