I have a Pandas dataframe with two indexes
Column1
indexA indexB
1001 aaa 1
bbb 1
ccc 1
ddd 1
created by
pd.read_sql(sql=sql, index_col=['indexA', 'indexB'])
MySQL is reading in indexB
as unicode and I would like to convert it to a string. My goal is to pivot the table and have the entries in indexB
be the column names. When I do this with the unicode values I get the following column names:
� � � � � � � � �
when running
pd.read_sql(sql=sql, index_col=['indexA', 'indexB']).unstack().fillna(0)
EDIT: A comment suggested the following:
df = pd.read_sql(sql)
df['indexB'] = df['indexB'].astype(str)
df = df.set_index(('indexA', 'indexB'), drop=True)
which is a nice work around to my problem (thank you). Would still be nice to know if this can be done during initialization.