'How to use pandas built-in functions to get dataframes for two month ranges
I have a dataframe with a date column, which contains the 'month', and I need to create two dataframes from this dataframe. the first one will contain all the listings for which the month values are from 1 to 6, and the second dataframe will contain all the listings for which the month values are from 7 to 12. How can I do this? I have tried this
train_set = data.loc(data['DateTime'] <= 6)
test_set = data.loc(data['DateTime'] > 6)
But, I am getting the following error :
TypeError: unhashable type: 'Series'
Why might I be getting this error? And what is a way that I can achieve what I am trying to do? The column 'DateTime' contains only the month value that I extracted from the original data which was in python datetime format.
Solution 1:[1]
Try with correct format of loc[].
train_set = data.loc[data['DateTime'] <= 6]
test_set = data.loc[data['DateTime'] > 6]
Solution 2:[2]
Alternatively to loc, you can as well achieve the result using:
train_set = data[data['DateTime'] <= 6]
test_set = data[data['DateTime'] > 6]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ACHRAF |
| Solution 2 | DF.Richard |
