source-destination
をキーとし、messages
を値として持つ辞書を使用しました。最初のデータフレーム、各質問、宛先として第1のメッセージを投稿した店舗、第2のメッセージをソースとして掲示した店舗、キー 'source-destination'の辞書にカウンタを追加します。データフレームへのPython辞書 - DataFrameコンストラクタが正しく呼び出されていない
辞書をデータフレームに変換しようとしていますが、このエラーメッセージが表示されます。 ValueError: If using all scalar values, you must pass an index
import pandas as pd
from itertools import permutations
df = pd.read_csv('example.csv', sep=';', engine='python')
messages = {} # the dictionary where results is going to be stored
student= set()
destination = False # a simple boolean to make sure message 2 follows message 1
for row in df: # iterate over the dataframe
student.add(row[2]) # collect students' name
if row[1] == 1: # if it is an initial message
destination = row[2] # we store students as destination
elif row[1] == 2 and destination: # if this is a second message
source = row[2] # store student as source
key = source + "-" + destination # construct a key based on source/destination
if key not in messages: # if the key is new to dictionary
messages[key] = 1 # create the new entry
else: # otherwise
messages[key] += 1 # add a counter to the existing entry
destination = False # reset destination
else:
destination = False # reset destination
# add the pairs of source-destination who didn't interact in the dictionnary
for pair in permutations(student, 2):
if "-".join(pair) not in messages:
messages["-".join(pair)] = 0
f1 = pd.DataFrame.from_dict(messages)
print(f1)
何故でしょうか?
ありがとうございます。
の可能性のある重複[IndentationError:任意外側インデントレベルと一致していないインデント解除](http://stackoverflow.com/questions/492387/indentationerror-unindent-does-not-match-any -outer-indentation-level) – IanS
あなたのpythonコードが間違ってインデントされているようです。おそらく最後の 'f1'の前のスペース?とにかくパンダとは関係ありません。 – IanS
@lanSありがとうございます!今私は別のエラーメッセージが表示されます:pandas.core.common.PandasError:DataFrameコンストラクタが正しく呼び出されていません!何かヒント? – Sheron