EN VI

Python - When reading a csv file through pandas, all columns become a single column?

2024-03-17 06:00:09
Python - When reading a csv file through pandas, all columns become a single column

I am unable to print a DataFrame from a CSV file. It was originally in text format, but I converted to csv through the online site. So the file looks like this: picture of my csv]

CSV code:

"clientId,timestamp,log"
"1,Jun 29 2023 09:36:44.933,00:07"
"36441418,Jun 29 2023 09:36:45.002,00:12 seconds"
"34836299,Jun 29 2023 09:36:44.190,00:83 seconds"
"36412881,Jun 29 2023 09:35:02.138,00:00 seconds"
"37464661,Jun 29 2023 09:36:44.705,00:18 seconds"
"1,Jun 29 2023 09:36:45.500,00:09 seconds"
"37455397,Jun 29 2023 09:36:46.221,00:15 seconds"
"38071830,Jun 29 2023 09:34:29.692,00:05 seconds"

python output: enter image description here

Solution:

Try this:

import pandas as pd

with open('a.csv', "r+", encoding="utf-8") as csv_file:
    content = csv_file.read()

with open('a.csv', "w+", encoding="utf-8") as csv_file:
    csv_file.write(content.replace('"', ''))

df = pd.read_csv('a.csv',delimiter=',')
print(df)

You have to first remove the double quotes as the others have suggested, then you can read your csv file and save it as a pandas dataframe.

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login