Creating a DataFrame From a File

Let's try to create a DataFrame object from the file.

We'll cover the following

Read a CSV file

In the real world, most data is stored in a file. The most common file type is a CSV. If you want to analyze the data, the first step is to read the data from the file. pandas supports multiple file types, like CSV, TSV, and Excel.

There are some commonly used parameters in the read_csv function.

  • sep: If sep is None, Python will automatically detect the separator. In this case, the separator is ,.
  • header: If no names are passed, the behavior is identical to header=0 and column names are inferred from the first line of the file. If the column names are passed explicitly, then the behavior is identical to the header=None.
  • names: List of column names provided by the caller. If the file contains the header row, you need to specify the header=0 to override it.
  • nrows: Specify how many rows you want to read.
  • usecols: Return a subset of the columns. You can pass the column names such as ["a", "b"]

The head() function is used to show the first n rows. If you don’t pass the parameters, the default value is 10.

Get hands-on with 1200+ tech skills courses.