Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
In a csv file, a number of comment texts are written that contain commas (,). Since in the csv file it defaults to commas by default, I'm going to use quotation marks to separate the columns, but I get the following error:
in section "What have you tried?"



Please guide me.
Thanks alot

What I have tried:

Python
df = pd.read_csv("train2.csv", encoding = "ISO-8859-1", error_bad_lines=False)
df_toxic = df.drop(['id', 'comment_text'], axis=1)

###########################################
error:
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 4404, in drop
'{} not found in axis'.format(labels[mask]))

KeyError: "['id' 'comment_text'] not found in axis"
###########################################
The csv file is also saved as follows:
id,"comment_text","toxic","severe_toxic","obscene","threat","insult","identity_hate"
0000997932d777bf,"Explanation Why the edits made under my username Hardcore Metallica Fan were reverted? They weren't vandalisms. just closure on some GAs after I voted at New York Dolls FAC. And please don't remove the template from the talk page since I'm retired now.89.205.38.27","0","0","0","0","0","0"
Posted
Updated 17-Sep-19 6:57am
v2
Comments
Richard MacCutchan 24-Aug-19 4:01am    
The columns are clearly separated by commas, not quotation marks. The quotation marks merely indicate that such fields are text values, not numbers. And there is nothing in the error message that is related to reading csv data.
helisa 24-Aug-19 4:12am    
Thank you
What does code need to be modified?
I want commas separated columns, so I can use commas between words in a text column.
Please guide me.
Richard MacCutchan 24-Aug-19 4:40am    
I already told you, the data is separated by commas, as you can clearly see. And the error is related to the drop method, which is part of the pandas library, and I am afraid I have no idea what it means.
helisa 24-Aug-19 6:23am    
Please, dear other programmers, answer me.

1 solution

Python has inbuilt csv module for this. Pandas is a dataframe generally used in as data-structure for famous machine learning libraries like scikit-learn.

This is how you write into a csv file. I have explained the code for your understanding.

Python
# SWAMI KARUPPASWAMI THUNNAI

import csv

# ROWS to be written

rows = [["A", "B"], ["C", "D"]]

# newline="" will not create additional \n
with open("file.csv", "w", newline="") as file:
    writer = csv.writer(file)
    for row in rows:
        writer.writerow(row)


and this how to read a file

Python
# SWAMI KARUPPASWAMI THUNNAI

import csv

# ROWS to read

# newline="" will not create additional \n
with open("file.csv", "r", newline="") as file:
    reader = csv.reader(file)
    reader = list(filter(None, reader)) # Remove blank lines if any
    for row in reader:
        print(row)


Output for reader:

['A', 'B']
['C', 'D']
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900