• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Pandas 101 / How To Save a Pandas Data Frame as CSV File?

How To Save a Pandas Data Frame as CSV File?

March 26, 2020 by cmdlinetips

In this tutorial, we will learn how to save a Pandas Dataframe as CSV or TSV file.

import pandas as pd
pd.__version__

Let us create two lists with some data.

education = ["Bachelor's", "Less than Bachelor's","Master's","PhD","Professional"]
salary = [110000,105000,126000,144200,96000]

We can make a Pandas dataframe using multiple lists. The idea is to create a Python dictionary with variable names as keys and the lists as values. And then use the dictionary as input to Pandas’ DataFrame function to a data frame.

# Create dataframe in one step
df = pd.DataFrame({"Education":education,
                  "Salary":salary})
df
	Education	Salary
0	Bachelor's	110000
1	Less than Bachelor's	105000
2	Master's	126000
3	PhD	144200
4	Professional	95967

How To Write Pandas DataFrame as CSV File?

Now that we have a data frame and ready to save as a file. We will first save the data frame as CSV file, i.e. Comma Separated Value file. We can use Pandas’ to_csv() function to write the dataframe as a CSV file.

# write a pandas dataframe to csv file
df.to_csv("education_salary.csv")

How To Write Pandas DataFrame as CSV File without rowindex?

If you look at the content of the dataframe, it has row index or row names. IN our example, we have 0,1,2, as row index. And you may not want to write the row index to the file.

By specifying the argument “index=False” inside to_csv() function, we can write a dataframe’s content to a CSV file without its row index.

# write a pandas dataframe to csv file without row index
df.to_csv("education_salary.csv", index=False)

How To Write Pandas DataFrame as TSV File?

We can use Pandas’ to_csv() function to write dataframe as Tab Separated Value or TSV file by specifying the argument for separator with sep=”\t”.

# write a dataframe to tsv file
df.to_csv("education_salary.tsv", sep="\t")

How To Write Pandas DataFrame as TSV File without index?

Similarly we can also specify “index=False” inside to_csv() function to save dataframe as TSV file without row index

# write a dataframe to tsv file without index 
df.to_csv("education_salary.tsv", sep="\t", index=False)

This post is part of the series on Pandas 101, a tutorial covering tips and tricks on using Pandas for data munging and analysis.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow To Read a CSV File as Pandas Data Frame? Default ThumbnailHow To Save Pandas Dataframe as Excel File? Default ThumbnailHow to Save Pandas Dataframe as gzip/zip File? Default ThumbnailHow To Create a Pandas Data Frame From Lists?

Filed Under: Pandas 101

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version