私はパンダのデータフレームを辞書に入れようとしています。パンダのデータフレームのリストを持つ辞書を作成するにはどうすればよいですか?
データフレームチャンクのリストを辞書に値として入れようとしましたが、Pythonは何の説明もせずにエラーを返します。ここで
は私がやろうとしているものです:
私はパンダのデータフレームの中にメッセンジャーchatlogのcsvファイルをインポートし、その日付で、それを分割し、リストにすべてを置くことができました。
このリストを繰り返し、さらに分割したいと思います。チャットが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 Traceback (most recent call last): File "C:/Users/Jaepil/PycharmProjects/test_pycharm/PYNEER_KatalkBot_-_CSV_to_Chunk.py", line 32, in key = table.loc[:, "Date"].dt.date[0].strftime("%Y-%m-%d") File "C:\Users\Jaepil\Anaconda3\lib\site-packages\pandas\core\series.py", line 601, in getitem result = self.index.get_value(self, key) File "C:\Users\Jaepil\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2477, in get_value tz=getattr(series.dtype, 'tz', None)) File "pandas_libs\index.pyx", line 98, in pandas._libs.index.IndexEngine.get_value (pandas_libs\index.c:4404) File "pandas_libs\index.pyx", line 106, in pandas._libs.index.IndexEngine.get_value (pandas_libs\index.c:4087) File "pandas_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas_libs\index.c:5126) File "pandas_libs\hashtable_class_helper.pxi", line 759, in pandas._libs.hashtable.Int64HashTable.get_item (pandas_libs\hashtable.c:14031) File "pandas_libs\hashtable_class_helper.pxi", line 765, in pandas._libs.hashtable.Int64HashTable.get_item (pandas_libs\hashtable.c:13975) KeyError: 0
何をしましたか? 最初は、シリーズオブジェクトをハッシュすることができないので、文字列に変更したというエラーが発生しました。ただし、異なるエラーが発生しています。
"Series objects are mutable and cannot be hashed" error
WOW。何を.... .... どうやってそれ?????? .locを使ってはいけないのですか?しかし、なぜ? とは何ですか.iatですか? – zebralamy
ここで 'loc'は選択列ではありません。 – jezrael
key = table ["Date"] .dat.iat [0] .strftime( "%Y-%m-%d") - >動作しません。 dt.strftime( "%Y-%m-%d")。iat [0] - > workedです。 – zebralamy