Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Finding True and False values with df.query

I have a simple question. I have concatenated two dataframes with consistent columns. One of the column is entitled ‘isForecast’ where the top dataframe has a value of False, and for the dataframe it is True (I am adding a forecast to actuals data). From this point I put the data through a sklearn regression which is working but when I try to unpack the forecast I get an error.

allData = pd.concat([actuals, forecast])
allData = adjustment(allData,periodLength) #### this is a seperate function but the 'isForecast'column remains consistent.

forecastOutput = allData.query('isForecast').copy() ## error here

The error I am getting doesn’t make sense. My index is a column entitled ‘datetime’ and is not being referenced.

None of [Index([False, False, False, False, False, False, False, False, False, False,\n …\n True, True, True, True, True, True, True, True, True, True],\n dtype=’object’, name=’datetime’, length=91968)] are in the [index]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any suggestions or comments

>Solution :

For me working your solution, alternative is compare by True:

allData = pd.DataFrame({'isForecast':[True, False, True]})

forecastOutput = allData.query('isForecast').copy()
print(forecastOutput)
   isForecast
0        True
2        True

forecastOutput = allData.query('isForecast == True').copy()
print(forecastOutput)
   isForecast
0        True
2        True

Or boolean indexing:

forecastOutput = allData[allData['isForecast']]
print(forecastOutput)
   isForecast
0        True
2        True

If values are not boolean:

allData = pd.DataFrame({'isForecast':['True', 'False', 'True']})

forecastOutput = allData.query('isForecast == "True"').copy()
print(forecastOutput)
  isForecast
0       True
2       True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading