Write to Excel File with Pandas Module - python

How could I write a list of items [1,2,3,4,5] to an excel file in a specific tab starting at a specific row and column location using the Pandas module? Does it involve the pandas.DataFrame.to_excel function, and do I need to convert my list into a dataframe before writing it to the excel file?
Would I make the list into a series first, and then convert the series into a dataframe, and then write the dataframe to the excel file?

Yes you will need to use 'to_excel'. The one line code below creates a list, converts it to a series, then converts it to a dataframe and creates and excel file
pd.Series([ i for i in range(10)]).to_frame().to_excel('test.xlsx')

Related

exporting and indexing csv file with pandas

I've created a csv file with the column names and saved it using pandas library. This file will be used to create a historic record where the rows will be charged one by one in different moments... what I'm doing to add rows to this csv previously created is transform the record to a DataFrame and then using to_csv() I choose mode = 'a' as a parameter in order to append this record to the existing file. The problem here is that I would like to see and index automatically generated in the file everytime I add a new row. I already know when I import this file as a DF, an index is generated automatically, but this is within the idle interface...when I open the csv with Excel for example...the file doesn't have an index.
While writing your files to csv, you can use set index = True in the to_csv method. This ensures that the index of your dataframe is written explicitly to the csv file

Python excel to csv copying column data with different header names

So here is my situation. Using Python I want to copy specific columns from excel spreadsheet into specific columns into a csv worksheet.
The pre-filled column header names are named differently in each spreadsheet and I need to use a sublist as a parameter.
For example, in the first sublist, data column in excel needs to be copied from/to:
spreadsheet csv
"scan_date" => "date_of_scan"
Two sublists as parameters: one of names copied from excel, one of names of where to paste into csv.
Not sure if a dictionary sublist would be better than two individual sublists?
Also, the csv column header names are in row B (not row A like excel) which has complicated things such as data frames.
So, ideally I would like to have sublists converted to arrays,
spreadsheet iterates columns to find "scan_date"
copies data
iterates to find "date_of_scan" in csv
paste data
moves on to the second item in the sublists and repeats.
I've tried pandas and openpyxl and just can't seem to figure out the approach/syntax of how to do it.
Any help would be greatly appreciated.
Thank you.
Clarification edit:
The csv file has some preexisting data within. Also, I cannot change the headers into different columns. So, if "date_of_scan" is in column "RF" then it must stay in column "RF". I was able to copy, say, the 5 columns of data from excel into a temp spreadsheet and then concatenate into the csv but it always moved the pasted columns to the beginning of the csv document (columns A, B, C, D, E).
It is hard to know the answer without seeing you specific dataset, but it seems to me that a simpler approach might be to simply make your excel sheet a df, drop everything except the columns you want in the csv then write a csv with pandas. Here's some psuedo-code.
import pandas as pd
df=pd.read_excel('your_file_name.xlsx')
drop_cols=[,,,] #list of columns to get rid of
df.drop(drop_cols,axis='columns')
col_dict={'a':'x','b':'y','c':'z'} #however you want to map you new columns in this example abc are old columns and xyz are new ones
#this line will actually rename your columns with the dictionary
df=df.rename(columns=col_dict)
df.to_csv('new_file_name.csv') #write new file
and this will actually run in python, but I created the df from dummy data instead of an excel file.
#with dummy data
df=pd.DataFrame([0,1,2],index=['a','b','c']).T
col_dict={'a':'x','b':'y','c':'z'}
df=df.rename(columns=col_dict)
df.to_csv('new_file_name.csv') #write new file

Mantain Storage of a list of strings in a cell

I am facing problem in storing a list of strings for my python application. I am looking for a solution to store my dataframe offline like a csv file with a cell containing a List of strings. Can someone suggest any data structure or file format where i may be able to easily access, edit and save my dataframe
i have tried to store the dataframe in a csv file but on reading the data from csv file converts the list of strings into a single string
My dataframe
Error Comments
["A1","A2","A3'] "Answer1"
["A3","A2","A1"] "Answer2"

How to insert a dictionary into an existing excel file with pandas

I have an existing excel file in documents which I open as a data frame with df = pd.read_excel(filename, index_col ='respID') and then I read and treat as a normal data frame.
I process some data and I then have two dictionaries whose key values are the same as the index values of the data frame.
I'm looking to insert these dictionaries into the excel file after a specific column. How would I go about doing this. Would it be better if they were just series?
I've seen solutions
How to write to an existing excel file without overwriting data (using pandas)? and Pandas Series to Excel but I don't think either answers my question totally.
Worst comes to worst I can add the dictionaries to the data frame where I want them and just overwrite the entire excel file or write a new one and replace it but that seems inelegant.
Thank you.

python cvs and xlrd: how to write csv file cell by cell

I used this method xls to csv converter to convert excel files(xls and xlsx) to csv files.
But in this example, it uses csv.write.writerow() method, and I cannot find any method related to write cell by cell from csv writer object.
So how can I write to csv cell by cell?
There isn't any built-in support for writing cell by cell (or column by column). Why do you want to do this anyway? If you are trying to convert an Excel file to CSV, it is simple enough to do it row by row. If for whatever reason you really just want to write a few specific cells, in a nonsequential manner, you have to manage those writes yourself, perhaps in a 2-dimensional array (a list of lists would work fine) and then write that data structure out row by row to the CSV.

Categories

Resources