我有一个熊猫数据框,如下面的代码所示。我正在尝试“重新采样” 用于获取票证列的每日计数的数据。它没有给出任何错误,但是 重新采样,而不是wokring。这是一个更大数据集的示例。我想成为 能够按天、周、月、季度等获取计数,但.resample选项不可用 没有给我一个解决方案。我做错了什么
import pandas as pd
df = pd.DataFrame([['2019-07-30T00:00:00','22:15:00','car'],
['2013-10-12T00:00:00','0:10:00','bus'],
['2014-03-31T00:00:00','9:06:00','ship'],
['2014-03-31T00:00:00','8:15:00','ship'],
['2014-03-31T00:00:00','12:06:00','ship'],
['2014-03-31T00:00:00','9:24:00','ship'],
['2013-10-12T00:00:00','9:06:00','ship'],
['2018-03-31T00:00:00','9:06:00','ship']],
columns=['date_field','time_field','transportation'])
df['date_field2'] = pd.to_datetime(df['date_field'])
df['time_field2'] = pd.to_datetime(df['time_field'],unit = 'ns').dt.time
df['date_time_field'] = df.apply(lambda df : pd.datetime.combine(df['date_field2'],df['time_field2']),1)
df.set_index(['date_time_field'],inplace=True)
df.drop(columns=['date_field','time_field','date_field2','time_field2'],inplace=True)
df['tickets']=1
df.sort_index(inplace=True)
df.drop(columns=['transportation'],inplace=True)
df.resample('D').sum()
print('\ndaily resampling:')
print(df)
我认为您忘记了将输出分配给变量,如:
您的代码也应该简化:
此外,我认为{}不是好的实践,请检查{a1}和{a2}
相关问题 更多 >
编程相关推荐