如何从python列表中找到增加的趋势

时间:2018-01-14 05:03:44

标签: python-3.x pandas-groupby

我想打印下面提供的数据,在 Jan和Mar,9月到12月之间的增长趋势 请你帮我解释一下这个逻辑。

我尝试过以下代码,但它不正确:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
month=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec']

count=[100,200,300,100,120,100,90,80,90,100,110,120]
dicta={'month':month,'count':count}
input_data=pd.DataFrame(dicta)
print(input_data)
flag=0
counter=[]

indexer=[]
for index in range(len(input_data['count'].values)-1):

print('The index is',index)
a=input_data.iloc[index,0]
b=input_data.iloc[index+1,0]
if b>a:
    flag=flag+1
    print('if loop flag:',flag)
elif flag<2:
    flag=0
    print('elif loop flag')
else:
    counter.append(flag)
    print('else flag',counter)
    flag=0
print(flag)

1 个答案:

答案 0 :(得分:0)

你不一定需要所有这些进口。

trendList = []
trendStart = 0
trendEnd = 0
valuePrev = 0

count = 0
for value in count:
   count += 1
   if count == 1:
       valuePrev = value
       trendStart = count
   elif valuePrev <= value:     #extend if equal or greater
       trendEnd = count
       valuePrev = value
   else:                        #end trend if current is less
       trendEnd = count - 1
       trendList.append([trendStart,trendEnd])
       trendStart = count
       valuePrev = value
for trend in trendList:
    print str(trend[0]) + " to " + str(trend[1])

注意这看起来很严格,不包括几个月