【问题标题】:How do I make a dictionary that has a list of pandas dataframes as a value?如何制作一个包含熊猫数据框列表作为值的字典?
【发布时间】: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

我做错了什么? 起初,我收到一个错误,即无法对系列对象进行哈希处理,因此我将其更改为字符串。但是,现在出现了一个不同的错误。

"Series objects are mutable and cannot be hashed" error

【问题讨论】:

    标签: python pandas dictionary dataframe


    【解决方案1】:

    我认为你需要:

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

    首先将strftime转换为strings,然后通过iat选择第一个值:

    key = table["Date"].dt.strftime("%Y-%m-%d").iat[0]
    

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

    key = table.iloc[0, df.columns.get_loc("Date")].strftime("%Y-%m-%d")
    

    【讨论】:

    • 哇。什么....你是怎么做到的??????我不应该使用 .loc 吗?但为什么?那是什么.iat?
    • loc 在这里不需要,因为选择列。
    • key = table["Date"].dt.iat[0].strftime("%Y-%m-%d") --> 不起作用。原来的 key = table["Date"].dt.strftime("%Y-%m-%d").iat[0] --> 有效。
    • 我能再问一件事吗? table 似乎是一个元组,它在 [0] 处有一个奇怪的值,在 [1] 处有所需的数据帧。 [0] 处的值是多少? Groupby 成功了,但为什么?
    • 当然,您需要添加 i,因为 groupby 返回元组 - 组名与表类似 for i, group in table.groupby(lambda x: table["time_diff"][x] &lt; chunk_tolerance ):
    猜你喜欢
    • 1970-01-01
    • 2021-10-22
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多