Reputation: 79
I have the Dataframe I changed to category type. but after saving and loading the type returned to float64
data = pd.DataFrame(barray, columns=['line 1', 'line 2', 'line 3', 'line 4', 'piece to move', 'place to occupy'])
data['possibility of win'] = y
for col in ['line 1', 'line 2', 'line 3', 'line 4', 'piece to move', 'place to occupy']:
data[col] = data[col].astype('category')
data['line 1'].dtype
output: CategoricalDtype(categories=['2000', '2001', '2002', '2010', '2011', '2012', '2020', '2021', '2100', '2101', '2102', '2110', '2112', '2120', '2121', '2200', '2201', '2202', '2210', '2211', '2212', '2220', '2221'], ordered=False)
data.to_csv('data.csv')
data = pd.read_csv("data.csv")
data['line 1'].dtype
output: dtype('int64')
Upvotes: 1
Views: 279
Reputation: 863166
It is expected, because in csv all data are saved like text.
Solution is add parameter dtype
in read_csv
:
#all columns set to categoricals
data = pd.read_csv("data.csv", dtype='category')
If want specify only some columns use dictionary:
cols = ['line 1', 'line 2', 'line 3', 'line 4']
d = dict.fromkeys(cols, 'category')
data = pd.read_csv("data.csv", dtype=d)
Upvotes: 1