In this lesson you’ll learn how to configure pandas options and settings at the interpreter startup.
Configure Options and Settings at Interpreter Startup
00:00 In this video on Pandas tricks, you’re going to learn how to configure Pandas options and settings at the interpreter startup. Pandas has a number of options and settings that you can change that will affect how DataFrames are printed and what errors and warnings you see.
If you find yourself changing options and settings often, it can be very helpful to set up a startup file that contains all these options every time you start your interpreter. In a new file, go ahead and import
pandas, and create a new function called
Pandas options use dot notation, so you can easily use a nested dictionary to set the correct values. First, let’s make a dictionary called
options, and in here, add an item for
'display', which will be another dictionary.
'expand_frame_repr' (expand frame representation), which will just affect how DataFrames wrap from page to page if they’re particularly large—so in this case, set that equal to
False. And set a
And you can shorten items in each cell by setting a
'max_seq_items' (max sequence items) to something like
50. One thing that I find myself always trying to change after the fact is the
'precision', so you can set how floats and decimals will round.
Now make another item called
'mode', and this’ll just contain
'chained_assignments', and set that equal to
None. All this controls are the warnings you see if you try to change the value of a copy of a DataFrame, as opposed to the DataFrame itself. All right, that’s it for our
That’ll just equal the
value that’s contained. Cool! So,
if __name__ == '__main__':
start(). And you can get rid of that function name to clean up your namespace by just deleting
start like that.
Now, open up the Python interpreter—and that’s not right, so let’s see.
No such key(s) […]
s should not be here, so let me close that out, go up, and let’s just change that to
'chained_assignment', which is the correct key.
This can be helpful if you have a number of different startup files that you use for different projects. And now opening the interpreter, everything works! So
import pandas as pd, and just to prove that it worked, let’s do
pd.get_option('display.max_rows'), which you set to
And just to make sure that nothing persists, let’s close out of the terminal, open up a new one, jump right into the Python interpreter,
import pandas as pd, and let’s do
pd.get_option('display.max_rows'), and you can see it’s back to
06:53 So, there you go! Now you know how to set up a startup file for Pandas that’ll run every time you start your interpreter. These are just a couple of examples of the options that you can set, so you should look at the Pandas documentation if you’re interested in seeing what else you can change. Thanks for watching.
Become a Member to join the conversation.