'while loop doesnt work on my pandas dataframe
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
