I think this is what you're looking for. As I said in the comments, it's probably not the right solution and it's definitely not idomatic for pandas DataFrames.
import pandas as pd
groups = data.groupby('Cluster')
#create a dictionary of dataframes, one
for each cluster
c_dict = {
k: pd.DataFrame(v) for k,
v in groups.groups.iteritems()
}
Save a Pandas DataFrame to one Excel worksheet.,Write Pandas DataFrames to multiple worksheets in a workbook.,Use the DataFrame.to_excel method to export your data,Initialize / Load data your Pandas DataFrame.
Before we start, we’ll need to import a few libraries into Python as shown below. Go ahead and type this Python 3 code into you favorite Python editor.
import pandas as pd
import openpyxl
import xlsxwriter
Now let’s create the data that we’ll be using in this tutorial
# define data as a dictionary data = ({ "language": ["Python", "C-Sharp", "Javascript", "PHP"], "avg_salary": [120, 100, 120, 80], "applications": [10, 15, 14, 20] }) # Create a Pandas DataFrame out of a Python dictionary df = pd.DataFrame.from_dict(data) # look at the Data print(df.head())
Now, we would like to export the DataFrame that we just created to an Excel workbook. Pandas has a very handy to_excel method that allows to do exactly that. Let’s use it:
df.to_excel("languages.xlsx")
We’ll first split our data to three different DataFrames:
#we define three lists S1 = data["language"] S2 = data["avg_salary"] S3 = data["applications"] # We then create three dataframes df1 = pd.DataFrame(S1, columns = ["language"]) df2 = pd.DataFrame(S2, columns = ["avg_salary"]) df3 = pd.DataFrame(S3, columns = ["applications"]) # We then group the dataframes into a list for more efficient processing dflist = [df1, df2, df3]
Now we’ll import multiple dataframes into Excel (in this case our three dfs to three different worksheets).
# We 'll define an Excel writer object and the target file Excelwriter = pd.ExcelWriter("languages_multiple.xlsx", engine = "xlsxwriter") #We now we 'll loop the list of dataframes for i, df in enumerate(dflist): df.to_excel(Excelwriter, sheet_name = "Sheet" + str(i + 1), index = False) #And finally we save the file Excelwriter.save()
how to create a dictionary of pandas dataframes, and return the dataframes into excel worksheets?,How to compare two dataframes with timestamps and create a dictionary of dataframes ? Python Pandas,how to create a pandas dataframe from a dictionary with column names as keys and values as rows where the values are 2-d array,How to create a new dataframe from a dictionary and a list in pandas
I think this is what you're looking for. As I said in the comments, it's probably not the right solution and it's definitely not idomatic for pandas DataFrames.
import pandas as pd
groups = data.groupby('Cluster')
#create a dictionary of dataframes, one
for each cluster
c_dict = {
k: pd.DataFrame(v) for k,
v in groups.groups.iteritems()
}
To write a single object to an Excel .xlsx file it is only necessary to specify a target file name. To write to multiple sheets it is necessary to create an ExcelWriter object with a target file name, and specify a sheet in the file to write to.,If you wish to write to more than one sheet in the workbook, it is necessary to specify an ExcelWriter object:,Multiple sheets may be written to by specifying unique sheet_name. With all data written to the file it is necessary to save the changes. Note that creating an ExcelWriter object with a file name that already exists will result in the contents of the existing file being erased.,To set the library that is used to write the Excel file, you can pass the engine keyword (the default engine is automatically chosen depending on the file extension):
>>> df1 = pd.DataFrame([
['a', 'b'],
['c', 'd']
],
...index = ['row 1', 'row 2'],
...columns = ['col 1', 'col 2']) >>>
df1.to_excel("output.xlsx")
>>> df1.to_excel("output.xlsx",
...sheet_name = 'Sheet_name_1')
>>> df2 = df1.copy() >>>
with pd.ExcelWriter('output.xlsx') as writer:
...df1.to_excel(writer, sheet_name = 'Sheet_name_1')
...df2.to_excel(writer, sheet_name = 'Sheet_name_2')
>>> with pd.ExcelWriter('output.xlsx',
...mode = 'a') as writer:
...df.to_excel(writer, sheet_name = 'Sheet_name_3')
>>> df1.to_excel('output1.xlsx', engine = 'xlsxwriter')
Last Updated : 16 Aug, 2021,GATE CS 2021 Syllabus
Output :
Name Age Stream Percentage
0 Ankit 18 Math 95
1 Rahul 19 Science 90
2 Shaurya 20 Commerce 85
3 Aishwarya 18 Math 80
4 Priyanka 19 Science 75
An example of writing multiple dataframes to worksheets using Pandas and XlsxWriter., Page Example: Pandas Excel with multiple dataframes ,Example: Pandas Excel with multiple dataframes,Pandas with XlsxWriter Examples
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # An example of writing multiple dataframes to worksheets using Pandas and # XlsxWriter. # # SPDX - License - Identifier: BSD - 2 - Clause # Copyright 2013 - 2022, John McNamara, jmcnamara @cpan.org # import pandas as pd # Create some Pandas dataframes from some data. df1 = pd.DataFrame({ 'Data': [11, 12, 13, 14] }) df2 = pd.DataFrame({ 'Data': [21, 22, 23, 24] }) df3 = pd.DataFrame({ 'Data': [31, 32, 33, 34] }) # Create a Pandas Excel writer using XlsxWriter as the engine. writer = pd.ExcelWriter('pandas_multiple.xlsx', engine = 'xlsxwriter') # Write each dataframe to a different worksheet. df1.to_excel(writer, sheet_name = 'Sheet1') df2.to_excel(writer, sheet_name = 'Sheet2') df3.to_excel(writer, sheet_name = 'Sheet3') # Close the Pandas Excel writer and output the Excel file. writer.save()
You can save or write a DataFrame to an Excel File or a specific Sheet in the Excel file using pandas.DataFrame.to_excel() method of DataFrame class.,Call to_excel() function on the DataFrame with the writer and the name of the Excel Sheet passed as arguments.,Open the excel file. Please note the name of the excel sheet. It is named to the string we specified as second argument to to_excel() function.,You can write the DataFrame to Excel File without mentioning any sheet name. The step by step process is given below:
The prerequisite to work with Excel file functions in pandas is that, you have to install openpyxl module. To install openpyxl using pip, run the following pip command.
pip install openpyxl
Python Program
import pandas as pd # create dataframe df_marks = pd.DataFrame({ 'name': ['Somu', 'Kiku', 'Amol', 'Lini'], 'physics': [68, 74, 77, 78], 'chemistry': [84, 56, 73, 69], 'algebra': [78, 88, 82, 87] }) # create excel writer object writer = pd.ExcelWriter('output.xlsx') # write dataframe to excel df_marks.to_excel(writer) # save the excel writer.save() print('DataFrame is written successfully to Excel File.')