Python 从DataFram转换为List后Col丢失
我有以下问题。。。我想分组(5小时)并计算时间戳。为此,我找到了以下问题/解决方案: 现在我想将其转换为一个列表,但在转换后,列表中只有计数。时间戳丢失了 我的代码:Python 从DataFram转换为List后Col丢失,python,pandas,dataframe,pandas-groupby,Python,Pandas,Dataframe,Pandas Groupby,我有以下问题。。。我想分组(5小时)并计算时间戳。为此,我找到了以下问题/解决方案: 现在我想将其转换为一个列表,但在转换后,列表中只有计数。时间戳丢失了 我的代码: d = {"count": timestamps} df = pd.DataFrame(data=d) df['timestamp'] = pd.to_datetime(df['count']) df = df.groupby(pd.Grouper(key='timestamp', freq="5H&
d = {"count": timestamps}
df = pd.DataFrame(data=d)
df['timestamp'] = pd.to_datetime(df['count'])
df = df.groupby(pd.Grouper(key='timestamp', freq="5H")).count()
print(df)
data = df.values.tolist()
print(data)
输出:
将索引转换为numpy数组时,df.values将删除该索引 根据您希望列表的顺序,您可以执行以下操作:
data = df.index.tolist() + df.values.tolist()
data = [[df.index.tolist()[x], df.values.tolist()[x]] for x in range(len(df.values))]
它将列出所有索引,然后列出所有计数
或者你可以:
data = df.index.tolist() + df.values.tolist()
data = [[df.index.tolist()[x], df.values.tolist()[x]] for x in range(len(df.values))]
列出[ind,count]对