Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas deprecated warning to_dict()

I'm getting this deprecated warning:

Using short name for 'orient' is deprecated. Only the options: ('dict', list, 'series', 'split', 'records', 'index') will be used in a future version. Use one of the above to silence this warning.

when using any of thise lines:

df.to_dict('records')
df.to_dict(orient='records')
df.to_dict(orientation='records')

pandas v1.1.3 python v3.7.1

like image 965
Luggie Böörgöör Avatar asked Nov 05 '20 10:11

Luggie Böörgöör


People also ask

How do you convert a DataFrame to a dictionary?

Use DataFrame. To convert pandas DataFrame to Dictionary object, use to_dict() method, this takes orient as dict by default which returns the DataFrame in format {column -> {index -> value}} . When no orient is specified, to_dict() returns in this format.

How do I add rows to a DataFrame?

You can create a DataFrame and append a new row to this DataFrame from dict, first create a Python Dictionary and use append() function, this method is required to pass ignore_index=True in order to append dict as a row to DataFrame, not using this will get you an error.


3 Answers

Warning says that "orient" is deprecated. use it like this:

df.to_dict('records')

Instead of using orient='', use any of these directly ('dict', list, 'series', 'split', 'records', 'index'), like:

df.to_dict('dict')

df.to_dict('list')

like image 94
MUK Avatar answered Oct 12 '22 05:10

MUK


I was confused with the same warning. Later I found I used "record" instead of "records". In any case, you can insert print(orient) line in the file "pandas/core/frame.py" at a line near 1485 right before the warning.

like image 5
Sergey Skripko Avatar answered Oct 12 '22 05:10

Sergey Skripko


I had the same warning when I used this code line: df.to_dict('records') but when I tried df.to_dict(orient = 'records'), the warning went away.

like image 1
pearl51 Avatar answered Oct 12 '22 07:10

pearl51