如何遍历列表?

时间:2019-07-18 04:12:06

标签: python pandas loops

我是Python的新手, 我想做一个简单的循环,遍历具有月份名称的列表:

我尝试了For循环,例如:

months=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
Max=[]
hours=[]
for m in months:
    time=m.count()/96  # when it goes to "Jan" it has 2976 elements
    for i in range(1,int(time)+1): 
        a=Rdata.m[Rdata.m['Day'] == i].dem.max() # Rdata.Jan is a df which has columsn Day and dem
        b=Rdata.m.loc[Rdata.m['dem']== a,'Time']
    Max.append(a)
    hours.append(b)

只需添加更多信息

我创建了一个列表:

month= (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)

其中

Jan = Series which contains  2976 elements

我不断收到错误消息“ count()至少接受1个参数(给定0个)”

2 个答案:

答案 0 :(得分:0)

Count用于对具有指定值的元素数进行计数。例如: 水果= ['苹果','香蕉','樱桃']

x = fruit.count(“ cherry”)

您应该使用len()而不是在那里计数

time = len(m)/ 96

答案 1 :(得分:0)

将值用双引号或单引号引起来时,它们是文字字符串,而不是序列变量。

假设您的代码已定义1月至12月的系列,则需要创建该系列的列表月份,如下所示:

months=[Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec]