How to read a txt with pandas, and correctly put it in a dataframe to be converted to Excel

Question:

I’m new to web scraping. I need to convert my scraped data into an .xlsx file.

I converted it to a .csv like txt file to be converted into Excel later on.

Txt looks like this:

info about energy levels in Brazil

I used pandas to dataframe info, came out like this:

dataframe result

In the first line, FURNAS row is set as an index row, I want it to be a normal row

I tried using index = None, setting a row of white characters, but I don’t want any index row or column, just an Excel table.

Asked By: RafaelNavarro22

||

Answers:

It seems like you want to convert your scraped data into an XLSX file without any index. To achieve this, you can try the following steps:

  1. Make sure you have the required libraries installed. You will need pandas and openpyxl. If you don’t have them installed, you can install them using:
pip install pandas openpyxl
  1. Assuming you have your data in a CSV-like txt file, first read the contents into a pandas DataFrame:
import pandas as pd

file_path = "your_file.txt"
df = pd.read_csv(file_path, delimiter=";")

# It's recommended to display the DataFrame to see its current structure
print(df.head())
  1. If the FURNAS row is set as the index row, reset the index to make it a regular row:
df.reset_index(inplace=True)
  1. Save the DataFrame to an Excel file without index and header (if you don’t want the column names as well):
output_file_path = "output.xlsx"
df.to_excel(output_file_path, index=False, header=None, engine='openpyxl')
  1. After running these steps, you should have an XLSX file without any index rows or columns. Be sure to replace your_file.txt with the correct filename and path of your txt file and output.xlsx with the desired output filename and path.
Answered By: Jordan Dimov