{"id":7177,"date":"2021-05-30T09:15:29","date_gmt":"2021-05-30T03:45:29","guid":{"rendered":"https:\/\/python-programs.com\/?p=7177"},"modified":"2021-11-22T18:40:43","modified_gmt":"2021-11-22T13:10:43","slug":"read-csv-file-to-dataframe-with-custom-delimiter-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/read-csv-file-to-dataframe-with-custom-delimiter-in-python\/","title":{"rendered":"Read Csv File to Dataframe With Custom Delimiter in Python"},"content":{"rendered":"
In this article, we will see what are CSV files, how to use them in pandas, and then we see how and why to use custom delimiter with CSV files in pandas.<\/p>\n
A simple way to store big data sets is to use CSV files (comma-separated files).CSV files contain plain text and is a well know format that can be read by everyone including Pandas. Generally, CSV files contain columns separated by commas, but they can also contain content separated by a tab, or underscore or hyphen, etc. Generally, CSV files look like this:-<\/p>\n
total_bill,tip,sex,smoker,day,time,size\r\n16.99,1.01,Female,No,Sun,Dinner,2\r\n10.34,1.66,Male,No,Sun,Dinner,3\r\n21.01,3.5,Male,No,Sun,Dinner,3\r\n23.68,3.31,Male,No,Sun,Dinner,2\r\n24.59,3.61,Female,No,Sun,Dinner,4<\/pre>\nHere we see different columns and their values are separated by commas.<\/p>\n
Use CSV file in pandas<\/h3>\n
read_csv() method is used to import and read CSV files in pandas. After this step, a CSV file act as a normal dataframe and we can use operation in CSV file as we use in dataframe.<\/p>\n
syntax:\u00a0 pandas.<\/span>read_csv<\/span>(<\/span>filepath_or_buffer, sep=<\/span>‘, ‘<\/span>, delimiter=<\/span>None<\/span>, header=<\/span>‘infer’<\/span>, names=<\/span>None<\/span>, index_col=<\/span>None<\/span>, ….<\/span>)<\/span><\/p>\n
','<\/code> is default separator in read_csv() method.<\/p>\n
Let see this with an example<\/p>\n
import pandas as pd\r\ndata=pd.read_csv('example1.csv')\r\ndata.head()<\/pre>\nOutput<\/p>\n