Press "Enter" to skip to content

Author: Erik Marsja

PhD in Psychology, Linköping University. Main interest is experimental and cognitive psychology. Enjoy programming in Python and R.

Rename Files in Python: A Guide with Examples using os.rename()

In this post, we are going to work with Python 3 to rename files. Specifically, we will use the Python module os to rename a file and rename multiple files.

First, we will rename a single file in 4 easy steps. After that, we will learn how to rename multiple files using Python 3. To be able to change the name of multiple files using Python can come in handy. For example, if we have a bunch of data files (e.g., .csv files) with long, or strange names, we may want to rename them to make working with them easier later in our projects (e.g., when loading the CSV files into a Pandas dataframe).

How to Save a Seaborn Plot as a File (e.g., PNG, PDF, EPS, TIFF)

In this short post, we will learn how to save Seaborn plots to a range of different file formats. More specifically, we will learn how to use the plt.savefig method save plots made with Seaborn to:

  1. Portable Network Graphics (PNG)
  2. Portable Document Format (PDF)
  3. Encapsulated Postscript (EPS)
  4. Tagged Image File Format (TIFF)
  5. Scalable Vector Graphics (SVG)

Pandas drop_duplicates(): How to Drop Duplicated Rows

In this post, we will learn how to use Pandas drop_duplicates() to remove duplicate records and combinations of columns from a Pandas dataframe. That is, we will delete duplicate data and only keep the unique values.

This Pandas tutorial will cover the following; what’s needed to follow the tutorial, importing Pandas, and how to create a dataframe fro a dictionary. After this, we will get into how to use Pandas drop_duplicates() to drop duplicate rows and duplicate columns.

How to Remove a Column in R using dplyr (by name and index)

In this remove a column in R tutorial, we are going to work with dplyr to delete a column. Here, we are going to learn how to remove columns in R using the select() function. Specifically, we are going to remove columns by name and by index.

Finally, we will also learn how to remove columns from R dataframes that start with a letter, or a word ends with a letter, or word, or contains a character (like the underscore).

How to Read a File in Python, Write to, and Append, to a File

In this tutorial, we are going to learn how to read a file in Python 3. After we have learned how to open a file in Python, we are going to learn how to write to the file and save it again.  In previous posts, we have learned how to open a range of different files using Python. For instance, we have learned how to open JSON, CSV, Excel, and HTML files using Pandas, and the json library. Here, however, we are going to open plain files (.txt) in Python.

How to use Pandas get_dummies to Create Dummy Variables in Python

In this post, we will learn how to use Pandas get_dummies() method to create dummy variables in Python. Dummy variables (or binary/indicator variables) are often used in statistical analyses as well as in more simple descriptive statistics. Towards the end of the post, there’s a link to a Jupyter Notebook containing all Pandas get_dummies() examples.

Dummy Coding for Regression Analysis

One statistical analysis in which we may need to create dummy variables in regression analysis. In fact, regression analysis requires numerical variables and this means that when we, whether doing research or just analyzing data, wishes to include a categorical variable in a regression model, supplementary steps are required to make the results interpretable.

Learn How to Calculate Descriptive Statistics in R the Easy Way

In this post, we will learn how to carry out descriptive statistics in R. After we have learned how to do this, we will learn how to create a nice latex table and how to save the summary statistics to a .csv file.

Why Descriptive Statistics?

Carrying out descriptive statistics, also known as summary statistics, is a very good starting point for most statistical analyses. It is, furthermore, a very good way to summarize and communicate information about the data we have collected.

Pipx: Installing, Uninstalling, & Upgrading Python Packages in Virtual Envs

In this post, we will learn how to use pipx. Specifically, we will learn how to use pipx to install Python packages. We will learn how to install pipx, use pipx to install packages, how to run Python packages from a temporary environment, how to uninstall packages, and upgrade packages using pipx.

How to Change the Size of Seaborn Plots

In this short tutorial, we will learn how to change the figure size of Seaborn plots. For many reasons, we may need to either increase the size or decrease the size, of our plots created with Seaborn.

When do We Need to Change the Size of a Plot?

One example, for instance, when we might want to change the size of a plot could be when we are going to communicate the results from our data analysis. In this case, we may compile the descriptive statistics, data visualization, and results from data analysis into a report, or manuscript for scientific publication.

FacetGrid Plot

Here, we may need to change the size so it fits the way we want to communicate our results. Note, for scientific publication (or printing, in general) we may want to also save the figures as high-resolution images.

How to Read & Write SPSS Files in R Statistical Environment

In this post we are going to learn 1) how to read SPSS (.sav) files in R, and 2) how to write to SPSS (.sav) files using R.  More specifically, here we are going to work with the following two R packages haven (from the Tidyverse) and foreign to:

  • Read a .sav file into an R dataframe
  • Writing an R dataframe to a .sav file