简体   繁体   中英

Creating a new pandas Dataframe from CSV file with no header

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:

在此处输入图片说明

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

在此处输入图片说明

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

您不需要DataFrame构造函数,因为read_csv输出显然是DataFrame (如果不使用squeeze=True ,则使用Series ):

frame=pd.read_csv(path)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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