Bilal Nadeem
Bilal Nadeem

Reputation: 19

Adding data to pandas dataframe with no header

I am trying to add a row(list of int) to pandas dataframe but i am somehow unable to append it. The thing is the dataframe has no header and everywhere the put data through specifying column names. I dont understand how to do it without header. Below is my dataframe named sheet

sheet = pd.read_csv("labels.csv",header = None) #sheet = [[1,1,1,2,2 ...]]

i want to append resulting list named simple_list = [1,2,3,4,2,1,1,1] to it.

Upvotes: 0

Views: 8496

Answers (3)

Rolando Galindo
Rolando Galindo

Reputation: 11

This work for me. sheet.appends returns a dataframe, you need to assign it to the same var or a new one to us it. If not "sheet" will have always the original CSV content

import pandas as pd

if __name__ == '__main__':
    sheet = pd.read_csv("labels.csv", header=None)
    sheet = sheet.append([[41, 42, 42, 44, 45]], ignore_index=True)

Upvotes: 0

xopclabs
xopclabs

Reputation: 11

You need to create a series out of simple_list.

simple_series = pd.Series(simple_list)

Then you can append a row to your dataframe with argument ignore_index=True:

sheet = sheet.append(simple_series, ignore_index=True)

Upvotes: 0

Sam Chats
Sam Chats

Reputation: 2321

Does the following work?

sheet = sheet.append(pd.DataFrame([[1,2,3,4,2,1,1,1]]), ignore_index=True)

Upvotes: 1

Related Questions