简体   繁体   English

使用 python 使用 excel 文件初始化 dataframe 列

[英]Initialize dataframe columns using an excel file using python

I have an excel file which has column list.我有一个包含列列表的 excel 文件。 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 Excel 文件(columnlist.xls)内容如下(这是一个例子,我还有很多列)

I would like to initialize a dataframe with these columns and later start appending the data.我想用这些列初始化 dataframe,然后开始附加数据。

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.我尝试循环通过 excel 文件并将它们分配给一个列表,然后使用 dataframe fn 进行分配,我知道我做错了什么,请帮助解决以下错误 - 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' 'NoneType' object 没有属性 'append'

Expected O/P Dataframe df will have all the columns.预期的 O/P Dataframe df 将包含所有列。

Instead of df_cols = list() try df_cols = [] .而不是df_cols = list()尝试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.此外,您不必将df_columns设置为等于df_columns.append()所以只需在 for 循环中删除它即可。

Also, you set df = pd.DataFrame() but never use the variable df anywhere else in your code此外,您设置df = pd.DataFrame()但绝不在代码中的其他任何地方使用变量df

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.您不清楚,您是想要一个带有列的新 df 还是一个新列表。 I choose the former.我选择前者。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM