Convert .data file to .csv

I'm using a data called 'adults.data', I need to work with that data as a '.csv' file. Can anyone guide me on how can I change the format? I tried opening the file in excel and then save it as csv, but the new file contains only one column containing all the '.data' columns.

Topic data csv dataset

Category Data Science


  1. Before loading the data make sure check the file format.
  2. Load the required libraries
  3. First read the excel file using 'pd.read' function save the file into csv format using function 'to_csv'

. example:

pp = pd.read_excel('paul_palmer.xlsx')


pp.to_csv('paul_palmer.csv', index=False)

  • One way is to convert .data file to excel/csv using Microsoft Excel which provides an option to get external data (use Data tab --> from Text). Check this video for demo

  • Other way you can utilize python to read .data files and directly work with dataframes

         import pandas as pd
         df = pd.read_csv("adults.data")
    

    (OR)

         df = pd.read_table("adults.data")
    

Another solution is to use pandas to read the file and then export it to an excel form as following:

import pandas as pd
df = pd.read_csv("file.data", sep=",", header=None)
df.to_excel("output.xlsx")  

You could read more here

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.