1

I used a dictionary containing source-destination as keys and messages as values. It loops over the first dataframe, for each question, store who posted 1st message as the destination, store who posted 2nd message as source, add a counter in dictionary at key 'source-destination'.

Now I am trying to convert dictionary to dataframe, but I get this error message 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)

Any idea why?

Thank you in advance.

6
  • 1
    Possible duplicate of IndentationError: unindent does not match any outer indentation level Commented May 11, 2017 at 13:23
  • 1
    It looks like your python code is wrongly indented. Maybe the space before f1 at the end? Nothing to do with pandas anyway. Commented May 11, 2017 at 13:24
  • @lanS Thanks!You're right! Now I got another error message: pandas.core.common.PandasError: DataFrame constructor not properly called! Any hint? Commented May 11, 2017 at 13:32
  • 1
    Hi @Giada, shall we go back on chat ? Commented May 11, 2017 at 13:37
  • 1
    My guess would be you need to from_dict now. Commented May 11, 2017 at 13:47

1 Answer 1

1

May be you have a mix between tab and space in your code. Try to remove all the tabs and replace them with spaces.

Same problem : IndentationError: unindent does not match any outer indentation level

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! the problem is fixed but now I got another one pandas.core.common.PandasError: DataFrame constructor not properly called! Any suggestion? @Sushii

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.