import pandas as pd
def city_data():
city = {'City' : pd.Series(['Ithaca', 'Willingboro', 'Holyoke', 'Abilene', 'New York']),
'Shape Reported': pd.Series(['Triangle', 'Other', 'Oval', 'Disk', 'Light']),
'State': pd.Series(['NY', 'NJ', 'CO', 'KS', 'NY']),
'Start Time': pd.Series(['6/1/1930 22:00', '6/30/1930 20:00', '2/15/1931 13:00', '6/1/1931 13:00', '4/18/1933 19:00' ])}
df = pd.DataFrame(city)
df['Start Time'] = pd.to_datetime(df['Start Time'])
return df
def time(df):
df['month'] = df['Start Time'].dt.month
common_month = df['month'].mode()
print(common_month)
time(df)
当我在Dataframe上面运行时,它显示一个错误字符串索引必须是整数。 我不知道怎么解决这个问题。你知道吗
在调用时间之前添加df初始化
`df=城市数据()
时间(df) `你知道吗
在这种情况下,我可以看到如下输出:
0.6
d类型:int64
代码的问题是
df
最初并不存在。您已经创建了city_data()
,但是您没有调用它:请尝试以下代码:
输出:
希望这能回答你的问题!!
相关问题 更多 >
编程相关推荐