tensor
tensor

Reputation: 3350

How to change pandas category type using a dtype dict?

Instead of doing:

for col in df.columns : 
   df[col]= df[col].astype('category')

I am doing this:

dtype0= {'brand': np.dtype('int64'),
 'category': np.dtype('int64'),
 'chain': np.dtype('int64'),
 'company': np.dtype('int64'),
 'date': np.dtype('O'),
 'dept':  pandas.types.dtypes.CategoricalDtype,
 'id': np.dtype('int64')}

df= df.astype(dtype0)

However, it does not work. Just wondering, how do I change into category using the dictionary?

Upvotes: 17

Views: 23127

Answers (3)

Aditya Rathi
Aditya Rathi

Reputation: 81

do something like this. swap your dataframe name in this below pasted code.

column_datatype_dict = df_or.dtypes.apply(lambda x: x.name).to_dict()

print(column_datatype_dict)

Upvotes: 0

tensor
tensor

Reputation: 3350

Previous answer is not correct. We can cast after creating the dataframe.

Solution is (for the record for other people stuck here): Pandas 0.19.1

dtype0= {'brand': 'int64',
 'category': 'int64',
 'chain': 'int64',
 'company': 'int64',
 'date': 'str',
 'dept':  'category',
 'id': 'int64'}
df= df.astype(dtype0)

casting works here.

Upvotes: 38

ℕʘʘḆḽḘ
ℕʘʘḆḽḘ

Reputation: 19395

you can only do that when you read in your data

data = pd.read_csv('mypath.csv', dtypes = mydict)

Upvotes: -6

Related Questions