Change Separator when Importing pandas DataFrame from CSV File in Python (Example Code)
This tutorial demonstrates how to use a different delimiter when importing a pandas DataFrame from a CSV file in the Python programming language.
Preparing the Example
import pandas as pd # Load pandas library |
import pandas as pd # Load pandas library
my_df = pd.DataFrame({'A':range(1, 5), # Construct new pandas DataFrame 'B':['a', 'c', 'a', 'b'], 'C':range(15, 11, - 1)}) print(my_df) # A B C # 0 1 a 15 # 1 2 c 14 # 2 3 a 13 # 3 4 b 12 |
my_df = pd.DataFrame({'A':range(1, 5), # Construct new pandas DataFrame 'B':['a', 'c', 'a', 'b'], 'C':range(15, 11, - 1)}) print(my_df) # A B C # 0 1 a 15 # 1 2 c 14 # 2 3 a 13 # 3 4 b 12
my_df.to_csv('my_df.csv', # Write exemplifying DataFrame to folder sep = ';') |
my_df.to_csv('my_df.csv', # Write exemplifying DataFrame to folder sep = ';')
Example: Specify Delimiter of pandas DataFrame Columns when Reading a CSV File
my_df_new = pd.read_csv('my_df.csv', # Read CSV file sep = ';') print(my_df_new) # Unnamed: 0 A B C # 0 0 1 a 15 # 1 1 2 c 14 # 2 2 3 a 13 # 3 3 4 b 12 |
my_df_new = pd.read_csv('my_df.csv', # Read CSV file sep = ';') print(my_df_new) # Unnamed: 0 A B C # 0 0 1 a 15 # 1 1 2 c 14 # 2 2 3 a 13 # 3 3 4 b 12
Further Resources & Related Tutorials
Below, you can find some additional resources on topics such as data inspection, indices, variables, and data conversion.