首页 > 解决方案 > 熊猫比较列

问题描述

我正在处理波士顿 Hubway 系统的数据,我正在尝试计算早上开始的游乐设施数量和晚上开始的游乐设施数量。我要么得到一个错误,要么得到两个变量的输出 0,早上和晚上。这是我现在拥有的:

# Importing the data:
data = []
trip = open("ExData-trips.csv")
print(trip.readline().split(",")

for row in trip:
    row = row.split(",")
    row = [convert(x) for x in row]
    data.append(row)

# Trying to divide the morning and evening times from the data above:
ride_times = [convert_date(row[3]) for row in data]
#pd.to_datetime(trip['start_date'].dt.hour

morning = 0
evening = 0

for time in trip:
    if time.hour < 12:
        morning += 1
    else:
        evening += 1
print(morning)
print(evening)

输出:0 和 0

我试过使用注释掉的行#pd.to_datetime(trip['start_date']).dt.hour,但我得到一个错误:

TypeError:“_io.TextIOWrapper”对象不可下标

抱歉,如果格式关闭,我第一次在这个论坛上提问。让我知道是否需要任何其他信息。

标签: pythonpandasdatetime

解决方案


推荐阅读