CSV to XML – How to Convert in Python?

Problem Formulation Input: You have some data in a CSV file stored in ‘my_file.csv’ where the first row is the header and the remaining rows are values associated to the column names in the header. Name,Job,Age,Income Alice,Programmer,23,110000 Bob,Executive,34,90000 Carl,Sales,45,50000 Desired Output: You want to store the data in an XML file ‘my_file.xml’ so that each … Read more

17 Ways to Read a CSV File to a Pandas DataFrame

πŸ’¬ Question: How to import a CSV file to a Pandas DataFrame in Python? This article will discuss the most interesting examples to read a CSV file to a Pandas DataFrame. If not specified otherwise, we use the following CSV file for all examples: my_file.csv: Name,Job,Age,Income Alice,Programmer,23,110000 Bob,Executive,34,90000 Carl,Sales,45,50000 Let’s get started! Example 1 – … Read more

Python – Convert CSV to List of Lists

Problem Formulation Given a CSV file (e.g., stored in the file with name ‘my_file.csv’). INPUT: file ‘my_file.csv’ 9,8,7 6,5,4 3,2,1 Challenge: How to convert it to a list of lists (=nested list), i.e., putting the row values into the inner lists? OUTPUT: Python list of lists [[9, 8, 7], [6, 5, 4], [3, 2, 1]] … Read more

Python Convert CSV to Parquet

import pandas as pd df = pd.read_csv(‘my_file.csv’) df.to_parquet(‘my_file.parquet’) Problem Formulation Given a CSV file ‘my_file.csv’. How to convert the file to a Parquet file named ‘my_file.parquet’? πŸ’‘ Info: Apache Parquet is an open-source, column-oriented data file format designed for efficient data storage and retrieval using data compression and encoding schemes to handle complex data in … Read more

Convert CSV to Dictionary in Python

The best way to convert a CSV file to a Python dictionary is to create a CSV file object f using open(“my_file.csv”) and pass it in the csv.DictReader(f) method. The return value is an iterable of dictionaries, one per row in the CSV file, that maps the column header from the first row to the … Read more

Convert CSV to Excel xlsx in Python

Problem Formulation πŸ’‘ Challenge: Given a CSV file. How to convert it to an excel file in Python? We create a folder with two files, the file csv_to_excel.py and my_file.csv. We want to convert the CSV file to an excel file so that after running the script csv_to_excel.py, we obtain the third file my_file.csv in … Read more

Convert CSV to JSON in Python

5 Easy Steps to Convert a CSV to a JSON File You can convert a CSV file to a JSON file by using the following five steps: Import the csv and json libraries Open the CSV as a file object in reading mode using the open(path_to_csv, ‘r’) function in a context manager (=with environment). Load … Read more