Python字典到dataframe-未正确调用dataframe构造函数
我使用了一个字典,其中包含Python字典到dataframe-未正确调用dataframe构造函数,python,pandas,dictionary,dataframe,Python,Pandas,Dictionary,Dataframe,我使用了一个字典,其中包含源-目的地作为键和消息作为值。它在第一个数据帧上循环,对于每个问题,将发布第一条消息的人存储为目标,将发布第二条消息的人存储为源,在字典中的键“source destination”处添加一个计数器 现在我正在尝试将字典转换为数据帧,但是我收到了这个错误消息 ValueError:如果使用所有标量值,则必须传递索引 import pandas as pd from itertools import permutations df = pd.read_csv('exa
源-目的地
作为键和消息
作为值。它在第一个数据帧上循环,对于每个问题,将发布第一条消息的人存储为目标,将发布第二条消息的人存储为源,在字典中的键“source destination”处添加一个计数器
现在我正在尝试将字典转换为数据帧,但是我收到了这个错误消息
ValueError:如果使用所有标量值,则必须传递索引
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)
知道为什么吗
提前感谢您。可能您的代码中混合了制表符和空格。 尝试删除所有选项卡并用空格替换它们
同样的问题:可能的重复看起来python代码缩进错误。可能是结尾处f1前面的空格?反正跟熊猫没关系。@lanS谢谢!你说得对!现在我收到另一条错误消息:pandas.core.common.PandasError:DataFrame构造函数调用不正确!有什么提示吗?嗨@Giada,我们回去好吗?我猜你现在需要从dict
开始。谢谢!这个问题已经解决了,但是现在我得到了另一个pandas.core.common.PandasError:DataFrame构造函数没有正确调用!有什么建议吗@寿司