append dictionary to data frame

PythonPython 3.xPandasDataframe

Python Problem Overview


I have a function, which returns a dictionary like this:

{'truth': 185.179993, 'day1': 197.22307753038834, 'day2': 197.26118010160317, 'day3': 197.19846975345905, 'day4': 197.1490578795196, 'day5': 197.37179265011116}

I am trying to append this dictionary to a dataframe like so:

output = pd.DataFrame()
output.append(dictionary, ignore_index=True)
print(output.head())

Unfortunately, the printing of the dataframe results in an empty dataframe. Any ideas?

Python Solutions


Solution 1 - Python

You don't assign the value to the result.

output = pd.DataFrame()
output = output.append(dictionary, ignore_index=True)
print(output.head())

Solution 2 - Python

The previous answer (user alex, answered Aug 9 2018 at 20:09) now triggers a warning saying that appending to a dataframe will be deprecated in a future version.

A way to do it is to transform the dictionary to a dataframe and the concatenate the dataframes:

output = pd.DataFrame()
df_dictionary = pd.DataFrame([dictionary])
output = pd.concat([output, df_dictionary], ignore_index=True)
print(output.head())

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncs0815View Question on Stackoverflow
Solution 1 - PythonalexView Answer on Stackoverflow
Solution 2 - PythonK. DoView Answer on Stackoverflow