miloshdrago
miloshdrago

Reputation: 127

How to eliminate the index value of columns and rows in a python dataframe?

I am trying to eliminate the column and the row which state the index of the values and replace the first value of my column by 'kw'.

Have tried .drop without success

def main():

    df_old_m, df_new_m = open_file_with_data_m(file_old_m, file_new_m)
    #combine two dataframes together
    df = df_old_m.append(df_new_m)
    son=df["son"]
    gson=df["gson"]
    #add son
    df_old_m = df["seeds"].append(son)
    #add gson
    df_old_m = df_old_m.append(gson)
    #deleted repeated values
    df_old_m = df_old_m.drop_duplicates()

    #add kw as the header of the list
    df_old_m.loc[-1] = 'kw'  # adding a row
    df_old_m.index = df_old_m.index + 1  # shifting index
    df_old_m.sort_index(inplace=True)

This gives me .xlsx output

Upvotes: 1

Views: 62

Answers (1)

Nick
Nick

Reputation: 3845

If kw is the column you want to be your new index, you can do this:

df.set_index('kw', inplace=True)

Upvotes: 1

Related Questions