【发布时间】:2018-05-07 04:25:45
【问题描述】:
我正在尝试将 pandas 数据框放入字典中,而不是相反。
我尝试将数据帧块列表作为值放入字典中,Python 返回错误而没有任何解释。
这就是我想要做的:
我将 messenger chatlog csv 文件导入到 pandas 数据框中,并设法按日期对其进行拆分,并将它们全部放在一个列表中。
现在我想遍历这个列表并进一步拆分它:如果聊天停止超过 15 分钟,它会被拆分成块。我想列出这些特定日期的聊天块的另一个列表,然后将它们放在字典中,其中键是日期,值是这些块的列表。
然后突然 Python 返回一个错误。下面是我被卡住并返回错误的地方。
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[:, "日期"].dt.date[0].strftime("%Y-%m-%d") getitem 中的文件“C:\Users\Jaepil\Anaconda3\lib\site-packages\pandas\core\series.py”,第 601 行 结果 = self.index.get_value(self, key) 文件“C:\Users\Jaepil\Anaconda3\lib\site-packages\pandas\core\indexes\base.py”,第 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) 关键错误:0
我做错了什么? 起初,我收到一个错误,即无法对系列对象进行哈希处理,因此我将其更改为字符串。但是,现在出现了一个不同的错误。
【问题讨论】:
标签: python pandas dictionary dataframe