Skip to content Skip to sidebar Skip to footer

Convert Data From .data File To .csv File And Put Data In Columns Using Pandas

I want to convert data from a .data file to a .csv file and put the data from the .data file in columns with values under them. However, the .data file has a specific format and I

Solution 1:

Sure you can do it with pandas. You just need to read first N lines (36 in your case) to use them as header and read rest of the file like normal csv (pandas good at it). Then you can save pandas.DataFrame object to csv.

Since your data splitted into adjacent lines, we should split DataFrame we've read on two and stack them one next to other (horizontaly).

Consider the following code:

import pandas as pd

COLUMNS_COUNT = 36# read first `COLUMNS_COUNT` lines to serve as a headerwithopen('data.data', 'r') as f:
    columns = [next(f).strip() for line inrange(COLUMNS_COUNT)]
# read rest of the file to temporary DataFrame
temp_df = pd.read_csv('data.data', skiprows=COLUMNS_COUNT, header=None, delimiter=';', skip_blank_lines=True)
# split temp DataFrame on even and odd rows
even_df = temp_df.iloc[::2].reset_index(drop=True)
odd_df = temp_df.iloc[1::2].reset_index(drop=True)
# stack even and odd DataFrames horizontaly
df = pd.concat([even_df, odd_df], axis=1)
# assign column names
df.columns = columns
# save result DataFrame to csv
df.to_csv('out.csv', index=False)

UPD: code updated to correctly process data splitted onto two lines

Post a Comment for "Convert Data From .data File To .csv File And Put Data In Columns Using Pandas"