Python | Trabajando con Pandas y XlsxWriter | Serie 1

Python Pandas es una biblioteca de análisis de datos. Puede leer, filtrar y reorganizar conjuntos de datos pequeños y grandes y generarlos en una variedad de formatos, incluido Excel.

Pandas escribe archivos de Excel utilizando los módulos XlsxWriter.

XlsxWriteres un módulo de Python para escribir archivos en el XLSX formato de archivo. Se puede usar para escribir texto, números y fórmulas en varias hojas de trabajo. Además, admite funciones como formato, imágenes, gráficos, configuración de página, filtros automáticos, formato condicional y muchos otros.

Código n.º 1: convertir un dataframe de Pandas en un archivo xlsx usando Pandas y XlsxWriter.

# import pandas as pd
import pandas as pd
  
# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Data': ['Geeks', 'For', 'geeks', 'is',
                               'portal', 'for', 'geeks']})
  
# Create a Pandas Excel writer
# object using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandasEx.xlsx', 
                   engine ='xlsxwriter')
  
# Write a dataframe to the worksheet.
df.to_excel(writer, sheet_name ='Sheet1')
  
# Close the Pandas Excel writer
# object and output the Excel file.
writer.save()

Producción :

 

Código n.º 2: escritura de varios marcos de datos en hojas de trabajo mediante Pandas y XlsxWriter.

# import pandas as pd
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 object 
# 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 object
# and output the Excel file.
writer.save()

Salida:

 
Código #3: Posicionamiento de marcos de datos en una hoja de trabajo usando Pandas y XlsxWriter.

# import pandas as pd
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]})
df4 = pd.DataFrame({'Data': [41, 42, 43, 44]})
  
# Create a Pandas Excel writer object
# using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_positioning.xlsx', 
                             engine ='xlsxwriter')
  
# write and Positioning the dataframes in the worksheet.
# Default position, cell A1.
df1.to_excel(writer, sheet_name ='Sheet1')  
df2.to_excel(writer, sheet_name ='Sheet1', startcol = 3)
df3.to_excel(writer, sheet_name ='Sheet1', startrow = 6)
  
# It is also possible to write the
# dataframe without the header and index.
df4.to_excel(writer, sheet_name ='Sheet1',
             startrow = 7, startcol = 4,
             header = False, index = False)
  
# Close the Pandas Excel writer object
# and output the Excel file.
writer.save()

Producción :
Output5

Publicación traducida automáticamente

Artículo escrito por ankthon y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *