while 循环不适用于我的 pandas 数据框
我有一个带有日期索引的熊猫数据框。形状是 100, 1。每次我对此运行 while 循环时,都会收到一条错误消息 ValueError: 值的长度 (100) 与索引的长度 (91460) 不匹配。我尝试了很多方法,这里给出我尝试过的一种方法。
start = 0
end = 3
result = [None] * 2 # because trend will start after the third value
while end <= len(mdf6.session_id_count_change):
if np.all(mdf6.session_id_count_change[start:end] > 0):
result.append("Increasing")
elif np.all(mdf6.session_id_count_change[start:end] < 0):
result.append("Decreasing")
else:
result.append(None)
start += 1
end += 1
df["Trend"] = result
错误日志:
~\AppData\Roaming\Python\Python39\site-packages\pandas\core\common.py in require_length_match(data, index) 555 """ 556 if len(data) != len(index): -- > 557 raise ValueError( 558 " 值的长度 " 559 f"({len(data)}) " ValueError: 值的长度(100) 与索引长度 (91460) 不匹配
有趣的是,我从不使用错误日志中显示的 if len(data) != len(index) 部分,在我的代码中的任何地方我都是Python新手,所以请耐心等待。如果我问一个明显的问题,总是要避免 while 循环,非常感谢。
I have a pandas dataframe with date index. The shape is 100, 1. Everytime I run a while loop on this, I get an error saying ValueError: Length of values (100) does not match length of index (91460). I tried many ways, giving here one of the ways I tried.
start = 0
end = 3
result = [None] * 2 # because trend will start after the third value
while end <= len(mdf6.session_id_count_change):
if np.all(mdf6.session_id_count_change[start:end] > 0):
result.append("Increasing")
elif np.all(mdf6.session_id_count_change[start:end] < 0):
result.append("Decreasing")
else:
result.append(None)
start += 1
end += 1
df["Trend"] = result
Error Log:
~\AppData\Roaming\Python\Python39\site-packages\pandas\core\common.py in require_length_match(data, index) 555 """ 556 if len(data) != len(index): --> 557 raise ValueError( 558 "Length of values " 559 f"({len(data)}) " ValueError: Length of values (100) does not match length of index (91460)
Interestingly, I never use if len(data) != len(index) part that shows up in the error log, in my code in any place. I am new to python, so please bear with me if I am asking an obvious question. It is very retrictive to always avoid a while loop. Any help in this regards is appreciated. Thanks a lot in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论