pandas dataframegroupby export to excel

  • Last Update :
  • Techknowledgy :
1._
df = pd.read_csv('log.csv', sep = '\t')
df = pd.DataFrame(df.groupby(['Country', 'Sub', 'Source']).size(), columns = ['Count'])

You'll get:

                     Count
                     Country Sub Source
                     IN 3 source4 1
                     UK 1 source3 1
                     US 1 source1 2
                     2 source2 1

then save to excel by:

df.to_excel('output.xls')

Suggestion : 2

According to the docs groupby returns a GroupBy object. The to_excel operation can only be performed on DataFrame. You can cast the GroupBy object to a DataFrame and then call the to_excel function,Pandas DataFrameGroupBy export to Excel,Python Pandas - loop through folder of Excel files, export data from each Excel file's sheet into their own .xlsx file,Cannot export Pandas dataframe to specified file path in Python for csv and excel both

1._
df = pd.read_csv('log.csv', sep = '\t')
df = pd.DataFrame(df.groupby(['Country', 'Sub', 'Source']).size(), columns = ['Count'])

You'll get:

                     Count
                     Country Sub Source
                     IN 3 source4 1
                     UK 1 source3 1
                     US 1 source1 2
                     2 source2 1

then save to excel by:

df.to_excel('output.xls')

Suggestion : 3

pandas.DataFrame.groupby , pandas.core.groupby.DataFrameGroupBy.pad , pandas.core.groupby.DataFrameGroupBy.mad , pandas.core.groupby.DataFrameGroupBy.cov

>>> 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')

Suggestion : 4

Today we’ll show you how to write and export data from a Pandas DataFrame to an Excel file (xlsx). We’ll deal with two scenarios:,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:,Save a Pandas DataFrame to one Excel worksheet.,First off, ensure that you have installed the pandas, openpyxl and xlsxwriter libraries into your environment. Here’s how to install Pandas in your Python development environment.

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()

Suggestion : 5

You can iterate on the object returned by groupby(). The iterator contains (Category, DataFrame) tuples., using transform to get group-level statistics while preserving the original dataframe , using transform to get group-level statistics while preserving the original dataframe , Reading files into pandas DataFrame

You can iterate on the object returned by groupby(). The iterator contains (Category, DataFrame) tuples.

# Same example data as in the previous example.
import numpy as np
import pandas as pd
np.random.seed(0)
df = pd.DataFrame({
   'Age': np.random.randint(20, 70, 100),
   'Sex': np.random.choice(['Male', factor 'Female'], 100),
   'number_of_foo': np.random.randint(1, 20, 100)
})

# Export to Male.csv and Female.csv files.
for sex, data in df.groupby('Sex'):
   data.to_csv("{}.csv".format(sex))