PANDAS Mac OS

broken image


Get the complete 19+ hour Udemy course here: https://www.udemy.com/data-analysis-with-pandas/?couponCode=FIFTEEN. Specifically what gave me trouble, was displaying the styled pandas html in an outlook email, as it just wouldn't render properly with the css mess that pandas was producing. Iterate over the dict and generate the html there by simply wrapping keys/values in the tags that you need, adding classes etc. And concatenate this all into one string. The simplest way to install not only pandas, but Python and the most popular packages that make up the SciPy stack (IPython, NumPy, Matplotlib, ) is with Anaconda, a cross-platform (Linux, macOS, Windows) Python distribution for data analytics and scientific computing.

  1. Pandas Mac Os Catalina
  2. Pandas Mask

Introduction

Using python and pandas in the business world can be a very useful alternativeto the pain of manipulating Excel files. While this combination of technologiesis powerful, it can be challenging to convince others to use a pythonscript - especially when many may be intimidated by using the command line.In this article I will show an example of how to easily createan end-user-friendly GUI using the Gooey library. This interface is basedon wxWindows so it looks like a 'native' application on Windows, Mac and Linux.Ultimately, I believe that presenting a simple user interface to your scripts can greatlyincrease the adoption of python in your place of business.

The Problem

PANDAS Mac OS

I will be basing the example in this article on my prior post - Combining Data From Multiple Excel Files.The basic concept is that there is a periodic need to combine data from multipleexcel files into a 'master file' and perform some additional manipulations.

Unfortunately this process is error prone and time consuming when someone tries todo a lot of manual copying and pasting in Excel. However, it is relativelystraightforward to create python + pandas scripts to perform thesame manipulations in a more repeatable and robust format. However, as soon asyou ask the user to type something like the line below, you will lose them:

Instead, you could present them with a simple GUI that looks something like this:

The nice thing about this example is that you have standard windows directory andfile chooser dialogs along with a standard date picker widget. It will be a muchsmoother transition for your users to use this UI than to try to remember howto use the command line with all the various arguments shown above.

The rest of this article will explain how to create this UI with very minorchanges to the base code you would build using argparse. If you arenot familiar with argparse then this article might be helpful to referencebefore you go much further. As shown in the article, argparse (and friends)are very sophisticated libraries but I have found that you can create verymeaningful and useful tools with the very basic options I'll show in this post.

Install

Gooey requires wxPython for its interface. wxPython can be a littletricky to install but if you are using the Anaconda or Miniconda distributionthe install is as simple as:

I highly recommend using conda for the install process - especially on Windows.

Gooey can then be installed using pip:

Building The Script

This notebook shows the basic idea for this program. What I will do next isbuild a simple version of this using argparse to pass in the sourceand destination directories as well as a location for the customer-status.xlsx file.

I am going to create a parse_args function to set up the following required inputs:

  • data_directory
  • output_directory
  • Customer account status file

I will add an example of an optional date argument as well but for the purposesof this example, I do not actually use the value. As they say, that is an exerciseleft to the reader.

The simplest example of argparse would look something like this:

When you are ready to access your arguments, you can get them like this:

One other unique aspect to this code is that I added a simple json dump of thearguments and restore them as the default next time the script is executed. Ioriginally did this to streamline the testing process but realize that this wouldbe helpful in the real world too. Here is the full code sample with the defaultvalues filled in based on the previous execution of the script.

This code allows us to do a basic command line interface that looks like this:

The main section of the code would look like the section below. The basic flow is:

  • Get the command line inputs
  • Pass the appropriate ones to the input and processing functions
  • Save the data to the desired location

Add a Gooey GUI

The command line solution shown above is very familiar to many but I imagine thereare people in your organization that would instantly turn away from a solution thatlooks something like what I have shown above. However, Gooey makes it as easy as two lines ofcode to make a UI for this script.

The most basic steps are to import Gooey and add the decorator in front of thefunction that processes your arguments. Here is what it would look for our example:

When you run this, you would see a simple UI like this:

I think we all agree that this is fairly intuitive and would be something youcould easily explain to your most non-technical users. The other nice thing isthat it runs the same on Windows, Mac or Linux (as illustrated above).

The one challenge would be that users would probably expect to have some nicewidgets to allow them to select directories and dates. If you would like to do thatthen you can substitute the GooeyParser for your ArgParser andadd the widget information to the parser code.

Change

to

And add your widget:

Here's what it looks like to use the DirChooser1, FileChooser andDateChooser widgets:

Now you have some native widgets the look very customary for the host OS:

The other nice feature is that when you execute the program you have a simplewrapper around the display and reasonable error windows if there is anunderlying error in your program.

One other handy component is that there is a 'Restart' button at the bottom of thescreen. If you select that button, you can go back to your input screen and adjust anyvariables and re-execute the program. This is really nice if you need to run theprogram multiple times with different inputs.

Part of what I really like about this solution is that there is very littleadditional overhead in your code. A traditional GUI (tkinter, QT, wxWindows etc)would require a lot of code to show this UI. This example showshow unobtrusive the solution can be.

The Final Program

I have not gone into the example of the actual pandas code but you can see in thefull sample that it is relatively straightforward to use the arguments asinputs into your various python function calls. If you would like to see thecode on github, here it is.

I think you will agree that this can be a really useful solution for distributingsmall standalone programs to users that are not comfortable running python from thecommand line. I have built this example around pandas but it would work for prettymuch any python script using argparse. Obviously if you need a more robust solutionthen you'll need to evaluate other options but I would argue that there is a lotof power in this potential solution.

Edit History

  • 9-14-2015: Corrected typos
  • 9-17-2015: Updated example code imports to include GooeyParser

Comments

Author

Bob Savage @mac.com>

Python on a Macintosh running Mac OS X is in principle very similar to Python onany other Unix platform, but there are a number of additional features such asthe IDE and the Package Manager that are worth pointing out.

4.1. Getting and Installing MacPython¶

Mac OS X 10.8 comes with Python 2.7 pre-installed by Apple. If you wish, youare invited to install the most recent version of Python 3 from the Pythonwebsite (https://www.python.org). A current 'universal binary' build of Python,which runs natively on the Mac's new Intel and legacy PPC CPU's, is availablethere.

What you get after installing is a number of things:

  • A Python3.9 folder in your Applications folder. In hereyou find IDLE, the development environment that is a standard part of officialPython distributions; and PythonLauncher, which handles double-clicking Pythonscripts from the Finder.

  • A framework /Library/Frameworks/Python.framework, which includes thePython executable and libraries. The installer adds this location to your shellpath. To uninstall MacPython, you can simply remove these three things. Asymlink to the Python executable is placed in /usr/local/bin/.

The Apple-provided build of Python is installed in/System/Library/Frameworks/Python.framework and /usr/bin/python,respectively. You should never modify or delete these, as they areApple-controlled and are used by Apple- or third-party software. Remember thatif you choose to install a newer Python version from python.org, you will havetwo different but functional Python installations on your computer, so it willbe important that your paths and usages are consistent with what you want to do.

IDLE includes a help menu that allows you to access Python documentation. If youare completely new to Python you should start reading the tutorial introductionin that document.

If you are familiar with Python on other Unix platforms you should read thesection on running Python scripts from the Unix shell.

4.1.1. How to run a Python script¶

Your best way to get started with Python on Mac OS X is through the IDLEintegrated development environment, see section The IDE and use the Help menuwhen the IDE is running.

If you want to run Python scripts from the Terminal window command line or fromthe Finder you first need an editor to create your script. Mac OS X comes with anumber of standard Unix command line editors, vim andemacs among them. If you want a more Mac-like editor,BBEdit or TextWrangler from Bare Bones Software (seehttp://www.barebones.com/products/bbedit/index.html) are good choices, as isTextMate (see https://macromates.com/). Other editors includeGvim (http://macvim-dev.github.io/macvim/) and Aquamacs(http://aquamacs.org/).

To run your script from the Terminal window you must make sure that/usr/local/bin is in your shell search path.

To run your script from the Finder you have two options:

  • Drag it to PythonLauncher

  • Select PythonLauncher as the default application to open yourscript (or any .py script) through the finder Info window and double-click it.PythonLauncher has various preferences to control how your script islaunched. Option-dragging allows you to change these for one invocation, or useits Preferences menu to change things globally.

4.1.2. Running scripts with a GUI¶

With older versions of Python, there is one Mac OS X quirk that you need to beaware of: programs that talk to the Aqua window manager (in other words,anything that has a GUI) need to be run in a special way. Use pythonwinstead of python to start such scripts.

With Python 3.9, you can use either python or pythonw.

4.1.3. Configuration¶

Python on OS X honors all standard Unix environment variables such asPYTHONPATH, but setting these variables for programs started from theFinder is non-standard as the Finder does not read your .profile or.cshrc at startup. You need to create a file~/.MacOSX/environment.plist. See Apple's Technical Document QA1067 fordetails.

For more information on installation Python packages in MacPython, see sectionInstalling Additional Python Packages.

4.2. The IDE¶

MacPython ships with the standard IDLE development environment. A goodintroduction to using IDLE can be found athttp://www.hashcollision.org/hkn/python/idle_intro/index.html.

4.3. Installing Additional Python Packages¶

There are several methods to install additional Python packages:

  • Packages can be installed via the standard Python distutils mode (pythonsetup.pyinstall).

  • Many packages can also be installed via the setuptools extensionor pip wrapper, see https://pip.pypa.io/.

4.4. GUI Programming on the Mac¶

There are several options for building GUI applications on the Mac with Python.

PyObjC is a Python binding to Apple's Objective-C/Cocoa framework, which isthe foundation of most modern Mac development. Information on PyObjC isavailable from https://pypi.org/project/pyobjc/.

The standard Python GUI toolkit is tkinter, based on the cross-platformTk toolkit (https://www.tcl.tk). An Aqua-native version of Tk is bundled with OSX by Apple, and the latest version can be downloaded and installed fromhttps://www.activestate.com; it can also be built from source.

wxPython is another popular cross-platform GUI toolkit that runs natively onMac OS X. Packages and documentation are available from https://www.wxpython.org.

PyQt is another popular cross-platform GUI toolkit that runs natively on MacOS X. More information can be found athttps://riverbankcomputing.com/software/pyqt/intro.

4.5. Distributing Python Applications on the Mac¶

The standard tool for deploying standalone Python applications on the Mac ispy2app. More information on installing and using py2app can be foundat http://undefined.org/python/#py2app.

4.6. Other Resources¶

Pandas Mac Os Catalina

The MacPython mailing list is an excellent support resource for Python users anddevelopers on the Mac:

Pandas Mask

Another useful resource is the MacPython wiki:





broken image