Python字典到dataframe - 没有正确调用DataFrame构造函数

时间:2017-05-11 13:21:04

标签: python pandas dictionary dataframe

我使用包含messages作为键的字典和 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)

{{1}}

知道为什么吗?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

您可能在代码中的选项卡和空格之间混合使用。 尝试删除所有选项卡并用空格替换它们。

同样的问题:IndentationError: unindent does not match any outer indentation level