简体   繁体   中英

equivalent of R's View for Python's pandas

The View is a very useful function to allow me to see cross-section of large data frames in R.

Is there any equivalent of R's View function for Python's pandas DataFrame ?

I use RStudio for R and PyCharm for Python.

A quicker option might be to set the pandas dataframe so it doesn't line wrap by putting this line of code:

import pandas
pandas.set_option('expand_frame_repr', False)

I'm using Sublime Text 2 and this is how it looks:

Before putting in option (Notice how the output wraps the text around)前

After putting in option (Notice how the output continues)后

Also make sure that 'View' > 'Word Wrap' is not checked.

Additionally, you can print out more or less as you need by using head(#) like this:

mydf = pandas.DataFrame.from_csv('myfile.csv', header=1)
print mydf.head(20) # Prints first 20 lines

Here's some other pandas options:

pandas.set_option('display.max_columns', 0) # Display any number of columns
pandas.set_option('display.max_rows', 0) # Display any number of rows

Spyder within Anaconda (or R Studio for Python as I like to call it) gives you the ability to view and sort entire dataframes the same way you would in R using the variable explorer.

https://www.continuum.io/

在此处输入图片说明

If you are a regular R user and using python also and you like R studio more then I would recommend you to use R Studio to write python scripts. You can use the reticulate library for the same. reticulate::conda_python() will take you to the python console and to write a script, just create new python script from the menu. Next consider the following code written in python:

import pandas as pd
df_python = pd.DataFrame({'num_legs': [2, 4, 8, 0],
               'num_wings': [2, 0, 0, 0],
               'num_specimen_seen': [10, 2, 1, 8]},
              index=['falcon', 'dog', 'spider', 'fish'])

This will create a pandas dataframe df_python

在此处输入图片说明

Now exit from the python console using exit keyword. Now when you will use py$ then you can access python objects. This can let you use this dataframe in R as well and hence you can view the dataframe also using View(py$df_python) and you will have the following output.

在此处输入图片说明

Keep Coding!

In ipython (notebook or qtconsole), you can do:

from IPython.display import HTML
HTML(myDataFrame.to_html())

Doesn't help with pycharm, but it may be worth pursuing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM