3

I am trying to read in a dictionary with two keys, the first with a string value and the second with a list of string values. The value containing a list can be (and often is) empty. For example:

{'number': '50', 'box': []}

However, when I try to use DataFrame.from_dict, it gives me an empty DataFrame. I notice that if the 'box' list has multiple elements, DataFrame.from_dict will give me a DataFrame with multiple rows, one for each value in the box list. This appears to be a sort of crossproduct behavior. Is there a way for me to suppress this behavior so that I can generate a DataFrame from the above example with one row, where the column "number" has value '50' and column "box" has value []?

I am using Pandas 0.16.2 and Python 2.7.10 via Anaconda 2.3.0 (64-bit Windows).

2 Answers 2

3

If you want to make a DataFrame with a single row, you can provide a list with a single dictionary:

df = pd.DataFrame([{'number': '50', 'box': []}])

The from_dict function expects a dict-of-lists, where the keys represent the columns and each value is a list (since DataFrame typically has more than one row) representing the values at each row. The following produces equivalent result using from_dict:

df = pd.DataFrame.from_dict({'number': ['50'], 'box': [[]]})
Sign up to request clarification or add additional context in comments.

Comments

0

The documentation page doesn't show many options for this method. Instead of an empty list you could pass it [np.NaN]:

df = pd.DataFrame.from_dict({'number': '50', 'box': [np.NaN]})

which will return a dataframe with one row.

Comments

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.