Python: Pandas, text file in DataFrame

I have a text file that looks like this: txt.file

I want to create a DataFrame from this, using space-separated columns, and use the letters at the top as column names, and then export the dataframe to excel. This is the code I've created so far:

import pandas as pd data = pd.read_csv('testrun.txt', delim_whitespace=True, header = None, names = ["n", "p", "d", "f", "g"]) df = pd.DataFrame(data) df.to_excel(r'C:\Users\"my_name"\Desktop\spdsht.xlsx') 

So far, this code sucks n, p, d, f, g into the data frame as another line. Anyway, can I omit this first row and add pandas as column names? As you can see, I already put the column names manually, but this does not eliminate n, p, d, f, g in the first row.

0
source share

Source: https://habr.com/ru/post/1267070/


All Articles