from pandas import Series, DataFrame
raw_data = {'': ['2018-01-01 12:00:00 AM', '2018-01-02 12:00:00 AM', '2018-01-03 12:00:00 AM', '2018-01-04 12:00:00 AM'],
'drop': [None, 1, None, None],
'collect': [None, None, None, 1]}
df = DataFrame(raw_data)
for row in df.iloc[0:df['drop'] ==True].etherrows(): #Calculate from the first row to the row where the drop value exists
If row['collect'] is True: If #collect value exists,
startpoint=row[0] #Save the time value at the start point
print(startpoint) #Expected result value: 2018-01-02 12:00:00 AM
for row in df.iloc[startpoint:df['collect'] ==True].iterrows(): Calculation from #startpoint row to row where collect value exists
If row['drop'] is True: If #drop value exists,
startpoint=row[0] #Save the time value at the start point
print(startpoint) #Expected result value: 2018-01-04 12:00:00 AM
I'd like to have the df file run like the code above. For row where the value in the df['drop'] column exists I want to store the row[0] value in the startpoint variable.
The error appears as below.
Traceback (most recent call last):
File "C:/Users/serec/PycharmProjects/180929/Stock+Pyson_Mr.O/For Questions.py", line 8, in <module>
for row in df.iloc[0:df['drop'] ==True].etherrows(): #Calculate from the first row to the row where the drop value exists
File "C:\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1478, in __getitem__
return self._getitem_axis(maybe_callable, axis=axis)
File "C:\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 2080, in _getitem_axis
return self._get_slice_axis(key, axis=axis)
File "C:\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 2048, in _get_slice_axis
slice_obj = self._convert_slice_indexer(slice_obj, axis)
File "C:\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 266, in _convert_slice_indexer
return ax._convert_slice_indexer(key, kind=self.name)
File "C:\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 1706, in _convert_slice_indexer
self._validate_indexer('slice', key.stop, kind),
File "C:\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 4145, in _validate_indexer
self._invalid_indexer(form, key)
File "C:\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 1863, in _invalid_indexer
kind=type(key)))
TypeError: cannot do slice indexing on <class 'pandas.core.indexes.range.RangeIndex'> with these indexers [0 False
1 True
2 False
3 False
Name: drop, dtype: bool] of <class 'pandas.core.series.Series'>
Process finished with exit code 1
The index of the first row that satisfies a condition in the dataframe can be obtained as follows.
end = df[df['drop'] == True].index[0]
Therefore, the dataframe from row to end is
df.iloc[0:end+1]
You can bring it like that. Then the code will change as follows.
from pandas import Series, DataFrame
raw_data = {'': ['2018-01-01 12:00:00 AM', '2018-01-02 12:00:00 AM', '2018-01-03 12:00:00 AM', '2018-01-04 12:00:00 AM'],
'drop': [None, 1, None, None],
'collect': [None, None, None, 1]}
df = DataFrame(raw_data)
end = df[df['drop'] == True].index[0]
For index, row in df.iloc[0:end+1]iterrows(): #Calculate for rows with drop value present
If row['collect'] is True: If #collect value exists,
startpoint=row[0] #Save the time value at the start point
print(startpoint) #Expected result value: 2018-01-02 12:00:00 AM
For index, row in df.iloc[0:end+1].iterrows(): #calculate from startpoint row to row where collect value exists
If row['drop'] is True: If #drop value exists,
startpoint=row[0] #Save the time value at the start point
print(startpoint) #Expected result value: 2018-01-04 12:00:00 AM
© 2024 OneMinuteCode. All rights reserved.