Skip to content
Advertisement

empty column in .toexcel() with pandas

i use pandas for python to generate a xlsx file with two other file. i take the first one as a template, and file the column with the second file. i wrote:

df_complete['<CLIENT>']='<CLIENT>'
df_complete['Code du client *']="C-"
df_complete['Raison sociale *']=df_client['Nom_Entreprise'].str.upper()
df_complete['Libellé régime de TVA (Soumis à la TVA, Franchise de TVA) *']='Soumis à la TVA'
df_complete['Gestion des factures récapitulatives (non, oui) * [non,oui]']='oui'
df_complete['Code de la devise *']='EUR'

but when i open my xlsx file the column ” and ‘Code du client *’ are empty, the other Columns are OK… I don’t know why it doesn’t run for the two first column…

Advertisement

Answer

The code is assigning constant values to columns, not setting the values of a new row. The syntax :

df_complete['<CLIENT>']='<CLIENT>'

Tries to set the value to all items in the Series. If that series is empty (as is the case here) nothing is set.

On the other hand, df_complete['Raison sociale *']=df_client['Nom_Entreprise'].str.upper() copies data from another dataframe that does contain some data.

To demonstrate :

import pandas as pd

df_complete=pd.DataFrame()
df_complete['<CLIENT>']='<CLIENT>'
df_complete['Code du client *']="C-"

df_complete 

Produces

Empty DataFrame
Columns: [<CLIENT>, Code du client *]
Index: []

Adding another series with values, only fills that series:

df_complete=pd.DataFrame()
df_complete['<CLIENT>']='<CLIENT>'
df_complete['Code du client *']="C-"
df_complete['Raison sociale *']=[1,2,3]

>>> df_complete 
  <CLIENT> Code du client *  Raison sociale *
0      NaN              NaN                 1
1      NaN              NaN                 2
2      NaN              NaN                 3

At the very least, rows should be added before setting series values:

df_complete=pd.DataFrame()
df_complete['Raison sociale *']=[1,2,3]
df_complete['<CLIENT>']='<CLIENT>'
df_complete['Code du client *']="C-"

>>> df_complete 
   <CLIENT> Code du client *  Raison sociale *
0  <CLIENT>               C-                 1
1  <CLIENT>               C-                 2
2  <CLIENT>               C-                 3
Advertisement