background preloader

Maths & stats

Facebook Twitter

Common Excel Tasks Demonstrated in Pandas - Practical Business Python. Introduction The purpose of this article is to show some common Excel tasks and how you would execute similar tasks in pandas. Some of the examples are somewhat trivial but I think it is important to show the simple as well as the more complex functions you can find elsewhere. As an added bonus, I’m going to do some fuzzy string matching to show a little twist to the process and show how pandas can utilize the full python system of modules to do something simply in python that would be complex in Excel. Make sense? Let’s get started. Adding a Sum to a Row The first task I’ll cover is summing some columns to add a total column.

We will start by importing our excel data into a pandas dataframe. import pandas as pdimport numpy as npdf = pd.read_excel("excel-comp-data.xlsx")df.head() We want to add a total column to show total sales for Jan, Feb and Mar. This is straightforward in Excel and in pandas. Next, here is how we do it in pandas: df["total"] = df["Jan"] + df["Feb"] + df["Mar"]df.head() Python et Excel. On m'a posé récemment la question des divers moyens de travailler avec Python et Excel. J'en connais trois : lecture, écriture offline de fichiers Excelcommuniquer avec Excel avec une API COMcommuniquer avec Excel en utilisant C# et .Net Je préfère de loin la première option car elle n'implique pas Excel et elle est portable.

Elle suppose juste la lecture et l'écriture de données selon le format des fichiers Excel. Et c'est la seule option qui marche si Excel n'est pas installé. Voici quelques modules qui permettent de faire cela : openpyxl : je l'utilise rarement tel quel mais quotidiennement avec pandas, son seul défaut est d'être assez lent pour des fichiers qui dépassent 100.000 lignes.tablib : ce module permet la manipulation des tables, ses sources incluent openpyxl, xlrd (plus rapide que openpyxl pour les gros fichiers), xlwt. La seconde option est celle que je déconseille. A python svg graph plotting library - Documentation. Data Science in Python.

Matplotlib: python plotting — Matplotlib 1.3.1 documentation.