1

I am trying to create a new dataframe from csv:

frame = DataFrame(data=pd.read_csv(path))

the result is correct except that the first line becomes the columns:

enter image description here

so I add columns to the dtaframe:

columns = ['person-id','time-stamp','loc-id']
frame = DataFrame(data=pd.read_csv(path),columns=columns)

then it goes wrong:the dataframe is all nan

enter image description here

this confuses me,can anyone tell me what is going on with it?

1
  • I've embedded your images for you. But, they are both the same, I don't think that is your intention. Commented Dec 7, 2016 at 12:13

2 Answers 2

1

You dont need DataFrame constructor, because output of read_csv is obviously DataFrame (if not use squeeze=True, then Series):

frame=pd.read_csv(path)
Sign up to request clarification or add additional context in comments.

6 Comments

that's a rapid bingo! so what is the problem with my expression?
Hmmm, first it seems same output and read_csv by default first row of csv convert to columns. So I am not sure what do you need exactly.
I had some mistakes adding a image,now it's fixed.would check it for me again? Why does dataframe turn nan?
It is another solution, but simply use frame = pd.read_csv(path, names=['person-id','time-stamp','loc-id']). I have same solution too, but then overwrite.
i get it. will be more careful when creating dataframe from csv
|
1

You need to tell read_csv() that your input has no column headers; by the time you give Dataframe the column names, it's too late. Try this:

columns = ['person-id','time-stamp','loc-id']    
frame = pd.read_csv(path, names=columns)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.