How to insert two columns in a CSV File using python? -


i want insert 2 columns in new csv file. question1 data should in first column , question2 data should in second column

below given code gives me output:

['question1']

['what', 'step', 'step', 'guide', 'invest', 'share', 'market', 'india']

['what', 'story', 'kohinoor', 'kohinoor', 'diamond']

['how', 'i', 'increase', 'speed', 'internet', 'connection', 'using', 'vpn']

['why', 'i', 'mentally', 'lonely', 'how', 'i', 'solve']

['which', 'one', 'dissolve', 'water', 'quikly', 'sugar', 'salt', 'methane', 'carbon', 'di', 'oxide']

['astrology', 'i', 'capricorn', 'sun', 'cap', 'moon', 'cap', 'rising', 'say']

['should', 'i', 'buy', 'tiago']

['how', 'i', 'good', 'geologist']

['when', 'use', 'ã‚·', 'instead', 'ã—']

['question2']

['what', 'step', 'step', 'guide', 'invest', 'share', 'market']

['what', 'would', 'happen', 'indian', 'government', 'stole', 'kohinoor', 'kohinoor', 'diamond', 'back']

['how', 'internet', 'speed', 'increased', 'hacking', 'dns']

['find', 'remainder', 'math', '23', '24', 'math', 'divided', '2423']

['which', 'fish', 'would', 'survive', 'salt', 'water']

['i', 'triple', 'capricorn', 'sun', 'moon', 'ascendant', 'capricorn', 'what', 'say']

['what', 'keeps', 'childern', 'active', 'far', 'phone', 'video', 'games']

['what', 'i', 'great', 'geologist']

['when', 'use', 'instead']

here code:

question1="\n".join(map(str, tokenized_reports_no_stopwords1)) question2="\n".join(map(str, tokenized_reports_no_stopwords2)) col1=question1.split("\n") col2=question2.split("\n") open("outputfile.csv" , mode="wt", encoding='utf-8') out_file:      w=csv.writer(out_file)      row in col1:          mycolumns = row.split("\n")          print(mycolumns)          w.writerow(mycolumns)      row in col2:          mycolumns = row.split("\n")          print(mycolumns)          w.writerow(mycolumns) 

the output should this: question1 should in first column of csv , question 2 should in second column of csv file

['question1'] ['question2']

['what', 'step', 'step', 'guide', 'invest', 'share', 'market', 'india'] ['what', 'step', 'step', 'guide', 'invest', 'share', 'market']

['what', 'story', 'kohinoor', 'kohinoor', 'diamond'] ['what', 'would', 'happen', 'indian', 'government', 'stole', 'kohinoor', 'kohinoor', 'diamond', 'back']

['how', 'i', 'increase', 'speed', 'internet', 'connection', 'using', 'vpn'] ['how', 'internet', 'speed', 'increased', 'hacking', 'dns']

['why', 'i', 'mentally', 'lonely', 'how', 'i', 'solve'] ['find', 'remainder', 'math', '23', '24', 'math', 'divided', '2423']

['which', 'one', 'dissolve', 'water', 'quikly', 'sugar', 'salt', 'methane', 'carbon', 'di', 'oxide'] ['which', 'fish', 'would', 'survive', 'salt', 'water']

['astrology', 'i', 'capricorn', 'sun', 'cap', 'moon', 'cap', 'rising', 'say'] ['i', 'triple', 'capricorn', 'sun', 'moon', 'ascendant', 'capricorn', 'what', 'say']

['should', 'i', 'buy', 'tiago'] ['what', 'keeps', 'childern', 'active', 'far', 'phone', 'video', 'games']

['how', 'i', 'good', 'geologist'] ['what', 'i', 'great', 'geologist']

['when', 'use', 'ã‚·', 'instead', 'ã—'] ['when', 'use', 'instead']

please me how can solve problem..

ypu can use pandas this.

import pandas pd  question1 = [['1', '1'], ['1', '2', '3'], ['3', '4']]   #question 1 data question2 = [['is', 'was'], ['i', 'am', 'me'],['yes', 'no']] #question 2 data  df = pd.dataframe(columns=["question1", "question2"]) df["question1"] = question1 df["question2"] = question2  df.to_csv("output.csv", index=false) 

output.csv

question1,question2 "['1', '1']","['is', 'was']" "['1', '2', '3']","['i', 'am', 'me']" "['3', '4']","['yes', 'no']" 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -