Reputation: 595
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).
Upvotes: 3
Views: 6820
Reputation: 14738
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': [[]]})
Upvotes: 3
Reputation: 2569
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.
Upvotes: 0