Editing
Code Guide
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Create a .csv file from a list of lists=== It was often useful to export data from the database to a .csv file for later retrieval. There are a few ways to do this. The first method uses the pandas package: <code> 1 import pandas as pd 2 3 column_data = [colA, colB, colC] 4 labels = [’A’, ’B’, ’C’] 5 6 df = pd.DataFrame(column_data) 7 df = df.transpose() # Remove this line if you want rows of data 8 df.columns = labels 9 pd.DataFrame.to_csv(df, ’filename.csv’, index = False) </code> To achieve the same result with default packages: <code> 1 labels = [’A’, ’B’] 2 colAdata = [some_data] 3 colBdata = [some_other_data] 4 RowsAsStrings = [ ] 5 6 # Convert data to strings 7 for i in range (0,len (longest_column)): 8 RowsAsStrings.append(str(colAdata[i]) + ’,’ + str(colBdata[i])) 9 10 # Write strings to a file 11 with open (’file.csv’, ’w+’) as file: 12 file.writelines(’labels’) 13 for i in data_text: 14 file.writelines(i + ’\n’) </code>
Summary:
Please note that all contributions to Derek may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Derek:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information