首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我如何制作一本字典,其中有熊猫数据列表作为一个值?

我如何制作一本字典,其中有熊猫数据列表作为一个值?
EN

Stack Overflow用户
提问于 2017-11-23 09:11:12
回答 1查看 143关注 0票数 1

我想把熊猫的资料放进字典里,而不是反过来。

我试图将数据块列表作为一个值放在字典中,Python返回一个错误而不作任何解释。

我想做的是:

我将信使聊天日志csv文件导入到熊猫的数据中,并设法将其按日期划分,并将它们都列在一个列表中。

现在,我想迭代这个列表,并将它分割得更多:如果聊天停止超过15分钟,它就会被分割成块。我想列出另一个列表,列出这些特定日期的聊天块,然后将它们放在字典中,其中键是日期,值是这些块的列表。

然后,Python突然返回一个错误。下面是我被困住的地方,并返回错误。

代码语言:javascript
运行
复制
import pandas as pd
from datetime import datetime

# Get chatlog and turn it into Pandas Dataframe
ktlk_csv = pd.read_csv(r'''C:\Users\Jaepil\PycharmProjects\test_pycharm/5years.csv''', encoding="utf-8")
df = pd.DataFrame(ktlk_csv)

# Change "Date" column from String to DateTime 
df["Date"] = pd.to_datetime(df["Date"])

# Make a column "time_diff" which is literally diffences of timestamp between chats. 
df["time_diff"] = df["Date"].diff()
df["time_diff"] = df["time_diff"].dt.total_seconds()

# Criteria to split chat chunks 
chunk_tolerance = 900 # 900: 15min of silence splits a chat
chunk_min = 5 # a chat less than 5 min is not a chunk. 

# Split a chatlog by date. (1st split)
df_byDate = []
for group in df.groupby(lambda x: df["Date"][x].day):
    df_byDate.append(group)

# Iterate over the list of splitted chats and split them into many chunks
df_chunk = {}
for day in df_byDate:
    table = day[1]
    list_of_daily_chunks = []
    for group in table.groupby(lambda x: table["time_diff"][x] < chunk_tolerance ):
        list_of_daily_chunks.append(group)

    # It does NOT return any error up to this point. 

    key = table.loc[:, "Date"].dt.date[0].strftime("%Y-%m-%d")
    df_chunk[key] = list_of_daily_chunks

这将返回一个错误:

C:/Users/Jaepil/PycharmProjects/test_pycharm/PYNEER_KatalkBot_-_CSV_to_Chunk.py跟踪(最近一次调用):"C:/Users/Jaepil/PycharmProjects/test_pycharm/PYNEER_KatalkBot_-_CSV_to_Chunk.py",第32行,以key = table.loc:,"Date".dt.date.strftime("%Y-%m-%d")文件"C:\Users\Jaepil\Anaconda3\lib\site-packages\pandas\core\series.py",第601行,在"C:\Users\Jaepil\Anaconda3\lib\site-packages\pandas\core\indexes\base.py",getitem = self.index.get_value(self,key)文件第2477行中,在get_value tz=getattr(series.dtype,'tz',None)文件"pandas_libs\index.pyx“中,第98行,在pandas._libs.index.IndexEngine.get_value (pandas_libs\index.c:4404)文件"pandas_libs\index.pyx“中,第106行,pandas._libs.index.IndexEngine.get_value (pandas_libs\index.c:4087)文件"pandas_libs\index.pyx”中,第154行,pandas._libs.index.IndexEngine.get_loc (pandas_libs\index.c:5126)文件"pandas_libs\hashtable_class_helper.pxi“中,第759行,在pandas._libs.hashtable.Int64HashTable.get_item (pandas_libs\hashtable.c:14031)文件"pandas_libs\hashtable_class_helper.pxi“中,第765行,在pandas._libs.hashtable.Int64HashTable.get_item (pandas_libs\hashtable.c:13975) KeyError: 0中

我做错了什么?一开始,我得到了一个错误,不能对系列对象进行散列,所以我将其更改为一个字符串。但是,现在出现了另一个错误。

"Series objects are mutable and cannot be hashed" error

EN

Stack Overflow用户

回答已采纳

发布于 2017-11-23 09:13:46

我觉得你应该:

代码语言:javascript
运行
复制
key = table.loc[:, "Date"].dt.date[0].strftime("%Y-%m-%d")

首先通过string转换为strftime,然后按iat选择第一个值

代码语言:javascript
运行
复制
key = table["Date"].dt.strftime("%Y-%m-%d").iat[0]

或使用iloc选择第一行,get_loc用于列Date的位置

代码语言:javascript
运行
复制
key = table.iloc[0, df.columns.get_loc("Date")].strftime("%Y-%m-%d")
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47451517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档