"train.csv"数据读入变量后,当我尝试使用train.head()打印前五行时,它给了我一个错误。

我的代码是:

train = pd.read_csv("train.csv",  dtype={'acoustic_data': np.int16, 'time_to_failure': np.float64})
train.head()


一旦运行它,我会收到错误消息:


  NameError追溯(最近的呼叫
  最后)
  ----> 1 train.head()
  
  NameError:名称“ train”未定义

最佳答案

我不知道出了什么问题,请尝试以下代码:

import pandas as pd
import numpy as np

train = pd.read_csv(r'http://samplecsvs.s3.amazonaws.com/Sacramentorealestatetransactions.csv',  dtype={'acoustic_data': np.int16, 'time_to_failure': np.float64})
print(train.head())


我得到:

>>>              street        city    zip state  beds  baths  sq__ft  \
>>> 0      3526 HIGH ST  SACRAMENTO  95838    CA     2      1     836
>>> 1       51 OMAHA CT  SACRAMENTO  95823    CA     3      1    1167
>>> 2    2796 BRANCH ST  SACRAMENTO  95815    CA     2      1     796
>>> 3  2805 JANETTE WAY  SACRAMENTO  95815    CA     2      1     852
>>> 4   6001 MCMAHON DR  SACRAMENTO  95824    CA     2      1     797
>>>
>>>           type                     sale_date  price   latitude   longitude
>>> 0  Residential  Wed May 21 00:00:00 EDT 2008  59222  38.631913 -121.434879
>>> 1  Residential  Wed May 21 00:00:00 EDT 2008  68212  38.478902 -121.431028
>>> 2  Residential  Wed May 21 00:00:00 EDT 2008  68880  38.618305 -121.443839
>>> 3  Residential  Wed May 21 00:00:00 EDT 2008  69307  38.616835 -121.439146
>>> 4  Residential  Wed May 21 00:00:00 EDT 2008  81900  38.519470 -121.435768


希望能有所帮助

关于python - 无法读取火车数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55914095/

10-16 00:51