Python属性错误 AttributeError 回溯(最近一次呼叫最后一次) 在() 3行程输入=['201309\u行程数据.csv'] 4 trip_out='201309_trip_summary.csv' 5总结数据(跳闸输入、车站数据、跳闸输出) 6. 7#加载数据文件并打印出前几行 在总结_数据时(在, 车站(数据,跳闸) 问题3a:在下面添加一个数学运算### 31####将持续时间从秒转换为分钟### 32新点['duration']=浮动(行['duration'])________ 33 34#将日期字符串重新格式化为多列 在这里输入代码 AttributeError:“float”对象没有属性“\uuuuuuuuuuuuuuuuuuuuuuuuuuu”
您的错误在第32行:Python属性错误 AttributeError 回溯(最近一次呼叫最后一次) 在() 3行程输入=['201309\u行程数据.csv'] 4 trip_out='201309_trip_summary.csv' 5总结数据(跳闸输入、车站数据、跳闸输出) 6. 7#加载数据文件并打印出前几行 在总结_数据时(在, 车站(数据,跳闸) 问题3a:在下面添加一个数学运算### 31####将持续时间从秒转换为分钟### 32新点['duration']=浮动(行['duration'])________ 33 34#将日期字符串重新格式化为多列 在这里输入代码 AttributeError:“float”对象没有属性“\uuuuuuuuuuuuuuuuuuuuuuuuuuu”,python,Python,您的错误在第32行:32新点['duration']=float(行['duration'])________您的代码类型将行['duration']的值强制转换为浮点数,然后使用句点()调用属性/或方法。代码中没有浮点属性'.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。正如Klaus D所指出的,看起来您使用的是一个模板,并且没有像您所期望的那样填写值。如果您可以更彻底地阅读模
32新点['duration']=float(行['duration'])________
您的代码类型将行['duration']
的值强制转换为浮点数,然后使用句点(
)调用属性/或方法。代码中没有浮点属性'.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。正如Klaus D所指出的,看起来您使用的是一个模板,并且没有像您所期望的那样填写值。如果您可以更彻底地阅读模板说明或在此处发布更多代码,您可能会得到更好的支持
看起来这一切都在函数中,summary_data()
,因此如果需要更多帮助,您可能应该从该函数中编写更多上下文
AttributeError
Traceback (most recent call last)
<ipython-input-15-568a9b76d0da> in <module>()
3 trip_in = ['201309_trip_data.csv']
4 trip_out = '201309_trip_summary.csv'
5 summarise_data(trip_in, station_data, trip_out)
6
7 # Load in the data file and print out the first few rows
<ipython-input-6-8a5a5140f3e9> in summarise_data(trip_in,
station_data, trip_out)
30### Question 3a: Add a mathematical operation below ###
31### to convert durations from seconds to minutes. ###
32new_point['duration'] = float(row['Duration']). ________
33
34# reformat datestrings into multiple columns
enter code here
AttributeError: 'float' object has no attribute '________'
任何测试成功的关键步骤都是理解任务。在这里,您似乎应该填写一些内容,但您没有填写。这是什么:new_point['duration']=float(row['duration'])________代码>?你的问题是什么?这个代码应该做什么?错误消息与您发布的代码片段有何关联?我添加了新的_点['duration']=float(行['duration'])/60。“问题3a:在下面添加一个数学运算,将持续时间从秒转换为分钟。”,“模板”是一个轻描淡写的说法。虽然此代码可能回答了这个问题,提供关于此代码为什么和/或如何回答此问题的附加上下文可提高其长期价值。
AttributeError
Traceback (most recent call last)
<ipython-input-15-568a9b76d0da> in <module>()
3 trip_in = ['201309_trip_data.csv']
4 trip_out = '201309_trip_summary.csv'
5 summarise_data(trip_in, station_data, trip_out)
6
7 # Load in the data file and print out the first few rows
<ipython-input-6-8a5a5140f3e9> in summarise_data(trip_in,
station_data, trip_out)
30### Question 3a: Add a mathematical operation below ###
31### to convert durations from seconds to minutes. ###
32new_point['duration'] = float(row['Duration']). ________
33
34# reformat datestrings into multiple columns
enter code here
AttributeError: 'float' object has no attribute '________'
def summarise_data(trip_in, station_data, trip_out):
"""
This function takes trip and station information and outputs a new
data file with a condensed summary of major trip information. The
trip_in and station_data arguments will be lists of data files for
the trip and station information, respectively, while trip_out
specifies the location to which the summarized data will be written.
"""
# generate dictionary of station - city mapping
station_map = create_station_mapping(station_data)
with open(trip_out, 'w') as f_out:
# set up csv writer object
out_colnames = ['duration', 'start_date', 'start_year',
'start_month', 'start_hour', 'weekday',
'start_city', 'end_city', 'subscription_type']
trip_writer = csv.DictWriter(f_out, fieldnames = out_colnames)
trip_writer.writeheader()
for data_file in trip_in:
with open(data_file, 'r') as f_in:
# set up csv reader object
trip_reader = csv.DictReader(f_in)
# collect data from and process each row
for row in trip_reader:
new_point = {}
# convert duration units from seconds to minutes
### Question 3a: Add a mathematical operation below ###
### to convert durations from seconds to minutes. ###
new_point['duration'] = float(row['Duration'])/60
# reformat datestrings into multiple columns
### Question 3b: Fill in the blanks below to generate ###
### the expected time values. ###
trip_date = datetime.strptime(row['Start Date'], '%m/%d/%Y %H:%M')
new_point['start_year'] = trip_date.strftime('%Y')
new_point['start_month'] = trip_date.strftime('%m')
new_point['start_hour'] = trip_date.strftime('%H')
new_point['weekday'] = trip_date.strftime('%A')
# remap start and end terminal with start and end city
new_point['start_city'] = station_map[row['Start Terminal']]
new_point['end_city'] = station_map[row['End Terminal']]
# two different column names for subscribers depending on file
if 'Subscription Type' in row:
new_point['subscription_type'] = row['Subscription Type']
else:
new_point['subscription_type'] = row['Subscriber Type']
# write the processed information to the output file.
trip_writer.writerow(new_point)
# Process the data by running the function we wrote above.
station_data = ['201402_station_data.csv']
trip_in = ['201309_trip_data.csv']
trip_out = '201309_trip_summary.csv'
summarise_data(trip_in, station_data, trip_out)
# Load in the data file and print out the first few rows
sample_data = pd.read_csv(trip_out)
display(sample_data.head())
# Verify the dataframe by counting data points matching each of the
time features.