Initialize dataframe columns using an excel file using python
Question:
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.
Answers:
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'])
You are not clear, whether you want a new df with the columns or a new list.
I choose the former.
for index, row in old_df.iterrows():
for col in df.columns:
new_df.loc[index, col] = row[col]
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.
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'])
You are not clear, whether you want a new df with the columns or a new list.
I choose the former.
for index, row in old_df.iterrows():
for col in df.columns:
new_df.loc[index, col] = row[col]