a1["合约值"]=a1["合约数"].astype("float")+a1["收盘价"].astype("float") a1.groupby(a1.index)["合约值"].apply(lambda x:x.value_counts()[0]) 报错KeyError: 0
时间: 2023-07-19 16:05:56 浏览: 47
这个错误出现的原因是 lambda 函数中的 value_counts() 方法返回的是一个 Series,而 Series 没有名为 0 的键,因此会报 KeyError。你可以使用 value_counts().index[0] 来获取最常见的值,即出现次数最多的值,也就是众数。修改后的代码如下:
```
a1["合约值"]=a1["合约数"].astype("float")+a1["收盘价"].astype("float")
a1.groupby(a1.index)["合约值"].apply(lambda x: x.value_counts().index[0])
```
这样就可以得到每个日期的合约值的众数了。
相关问题
if a1_choice == "Cooperate" and a2_choice == "Cooperate": a1.score += 3 a2.score += 3 elif a1_choice == "Cooperate" and a2_choice == "Defect": a1.score += 0 a2.score += 5 elif a1_choice == "Defect" and a2_choice == "Cooperate": a1.score += 5 a2.score += 0 elif a1_choice == "Defect" and a2_choice == "Defect": a1.score += 1 a2.score += 1 这段代码的意思
这段代码是一个经典的囚徒困境博弈的计分规则。在这个博弈中,有两个玩家(a1和a2),每个玩家可以选择“合作”或“背叛”。如果两个玩家都选择“合作”,则双方得分都加3分;如果一个玩家选择“合作”,而另一个玩家选择“背叛”,则选择“合作”的玩家得分为0分,选择“背叛”的玩家得分为5分;如果两个玩家都选择“背叛”,则两个玩家的得分都加1分。此代码就是根据玩家的选择,计算其得分的规则。
a1.sources = s1 a1.channels = c1 a1.sinks = k1 a1.sources.s1.type = spooldir a1.sources.s1.spoolDir = /opt/test/a1 a1.channels.c1.type = memory a1.channels.c1.capacity = 1000 a1.channels.c1.transactionCapacity = 100 a1.sinks.k1.type = hdfs a1.sinks.k1.hdfs.path = /flume/events/%Y-%m-%d/%H%M/%S a1.sinks.k1.hdfs.filePrefix = events- a1.sinks.k1.hdfs.round = true a1.sinks.k1.hdfs.roundValue = 10 a1.sinks.k1.hdfs.roundUnit = minute a1.sinks.k1.hdfs.useLocalTimeStamp = true a1.sources.s1.channels = c1 a1.sink.k1.channels = c1
这是一个Flume的配置文件,其中定义了一个名为"a1"的Agent,包含一个Spooldir Source、一个Memory Channel和一个HDFS Sink。
Spooldir Source会监控指定目录下的新文件,并将文件内容作为事件发送到Channel中。
Memory Channel是一个基于内存的Channel,用于临时存储事件。
HDFS Sink会将事件写入HDFS中的指定路径,其中`%Y-%m-%d/%H%M/%S`是时间戳格式,用于将事件按时间分隔存储。
其他的配置项包括Channel的容量、事务容量和Sink的一些参数,如文件前缀、轮换机制等。
最后,Source和Sink都指定了使用同一个Channel,即Channel "c1"。