Home > Blockchain >  Initialize dataframe columns using an excel file using python
Initialize dataframe columns using an excel file using python

Time:07-29

I have an excel file which has column list. Excel file (columnlist.xls) contents are as follows (This is an example, I have a lot more columns) Columns <-- header row FirstName LastName StreetAddress City State

I would like to initialize a dataframe with these columns and later start appending the data.

I tried looping through excel file and assign those to a list and then use dataframe fn for assignment, I know I am doing something wrong, please help with below error - TIA.

    import pandas as pd
    df = pd.DataFrame()
    df_cols = list()
    
    # read the saved columns in .csv file
    dfRepeatingColumns    = pd.read_excel('columnlist.xls', sheet_name ='Repeating')

    for index, row in dfNonRepeatingColumns.iterrows():
        df_cols = df_cols.append(row['Columns'])

Error:

'NoneType' object has no attribute 'append'

Expected O/P Dataframe df will have all the columns.

CodePudding user response:

for index, row in df.iterrows():
   for col in df.columns:
      df.loc[index, col] = row[col]

CodePudding user response:

Instead of df_cols = list() try df_cols = []. Also, you don't have to set the df_columns equal to df_columns.append() so just remove that in your for loop.

Also, you set df = pd.DataFrame() but never use the variable df anywhere else in your code

import pandas as pd

df_cols = []
 # read the saved columns in .csv file
dfRepeatingColumns    = pd.read_excel('columnlist.xls', sheet_name ='Repeating')

for index, row in dfNonRepeatingColumns.iterrows():
   df_cols.append(row['Columns'])
  • Related