要求:对已生成的一组dataframe数据列,需要根据日期做条件判断,对不同日期范围的数据,以不同的规则进行数据类运算,运算结果添加至一个新的列。
...
a=pd.DataFrame(data1,columns=['end_date','province','price']) #数组a
b=pd.DataFrame(data2,columns=['province','index_2013','index_2014','index_2015','index_2016'])#数据b
c=pd.merge(a,b,on='province') #以province进行连接
if a['end_date']<= datetime.strptime('2013-12-31','%Y-%m-%d').date(): #判断日期小于13年
c['axp']=c['price']c['index_2013'] #数据c添加列,数据值=priceindex_2013
print c
对if语句行报错,报错如下:ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
在报错行,条件后添加.any()或者.all()则报错消失,但得到的数据不是想要的结果如:if a['end_date'].any()<= datetime。。。 则不报错
迷茫2017-04-18 09:49:44
You can use the apply
function to perform horizontal or vertical calculations on the dataframe object
For example, suppose you do an if on the first column of your c and calculate the quotient of the last two columns:
def cal(x):
if x[0] > 10: # 对第一列筛选
return x[-1] / x[-2]
c.apply(cal, axis=1)
At this point, you can get the calculation result that meets the filtering conditions, which is a Series object
PHP中文网2017-04-18 09:49:44
This is a basic Python question.
if a['end_date']<= datetime.strptime('2013-12-31','%Y-%m-%d').date()
The vectorized calculation of a Series still returns the Series. The Series is an np array and does not provide a method to return a true value. Comparison is possible only after using .any()
,.all()
.
Then study the Pandas documentation carefully.