Search code examples
pandasposition

TypeError: can only concatenate str (not "list") to str - pandas


I'm working on a personal project, and I need to delete some rows from my dataframe, and the easiest way I found is to move a column to the position 0, then remove the rows whose 'headline' contain certain values.

import pandas as pd 
HC=pd.read_excel('Headcount 2020.xlsx')
cols=list(HC.columns)
HC=HC[cols[7]+[cols[0:7]]+cols[8:44]]

By doing this I got the error

TypeError: can only concatenate str (not "list") to str

However, in the example I was following this morning, with the same code, it worked perfectly

    import pandas as pd 
    df=pd.read_csv('pokemon_data.csv')
    df['Total'] = df.iloc[:,4:10].sum(axis=1)
    cols=list(df.columns)
    df=df[cols[0:4]+[cols[-1]]+cols[4:12]]

I ignore what it can be. Thanks in advance.


Solution

  • Use:

    HC=HC[[cols[7]]+cols[0:7]+cols[8:44]]
    

    the problem is that cols[7] is a str

    #HC=HC[cols[7]+[cols[0:7]]+cols[8:44]]