dataframe from dict resulting in empty dataframe

pandas dataframe from dict
pandas dataframe from dict of dicts
pandas dataframe from list of dicts
pandas unpack dictionary
valueerror: if using all scalar values, you must pass an index
create empty dataframe pandas with size
add dictionary to pandas dataframe
dataframe from dict of series

Hi I wrote some code that builds a default dictionary

def makedata(filename):
    with open(filename, "r") as file:
        for x in features:
            previous = []
            count = 0
            for line in file:
                var_name = x
                regexp = re.compile(var_name + r'.*?([0-9.-]+)')
                match = regexp.search(line)
                if match and (match.group(1)) != previous:
                    previous = match.group(1)
                    count += 1
                    if count > wlength:
                        count = 1
                    target = str(str(count) + x)
                    dict.setdefault(target, []).append(match.group(1))
            file.seek(0)

df = pd.DataFrame.from_dict(dict)

The dictionary looks good but when I try to convert to dataframe it is empty. I can't figure it out

dict: {'1meanSignalLenght': ['0.5305184', '0.48961428', '0.47203177', '0.5177274'], '1amplCor': ['0.8780955002105448', '0.8634431017504487', '0.9381169983046714', '0.9407036427333355'], '1metr10.angle1': ['0.6439386643584522', '0.6555194964997434', '0.9512436169922103', '0.23789348400794422'], '1syncVar': ['0.1344131181025432', '0.08194580887223515', '0.15922251165913678', '0.28795644612520327'], '1linVelMagn': ['0.07062673289287498', '0.08792496681784517', '0.12603999663935528', '0.14791253129369603'], '1metr6.velSum': ['0.17850601560734558', '0.15855169971072014', '0.21396496345720045', '0.2739525279330513']}

df:

Empty DataFrame
Columns: []
Index: []
{}

I think part of your issue is that you are using the keyword 'dict', assuming it is a variable

make a dictionary in your function, call it something other than 'dict'. Have your function return that dictionary. Then when you make a dataframe use that return value. Right now, you are creating a data frame from an empty dictionary object.

pandas.DataFrame.from_dict, If the keys of the passed dict should be the columns of the resulting DataFrame, pass 'columns' (default). Otherwise if the keys should be rows, pass 'index'. Creates DataFrame object from dictionary by columns or by index allowing dtype specification. Parameters data dict. Of the form {field : array-like} or {field : dict}. orient {‘columns’, ‘index’}, default ‘columns’ The “orientation” of the data. If the keys of the passed dict should be the columns of the resulting DataFrame

df = pd.DataFrame(dict)

This should make a dataframe from the dictionary.

pandas.DataFrame.empty, If DataFrame contains only NaNs, it is still not considered empty. See the example below. Examples. An example of an actual empty DataFrame. Notice the index  Create Pandas DataFrame from Python Dictionary. You can create a DataFrame from Dictionary by passing a dictionary as the data argument to DataFrame() class. In this tutorial, we shall learn how to create a Pandas DataFrame from Python Dictionary. Syntax – Create DataFrame. The syntax to create a DataFrame from dictionary object is shown below.

You can either pass a list of dicts simply using pd.DataFrame(list_of_dicts) (use pd.DataFrame([dict]) if your variable is not a list) or a dict of list using pd.DataFrame.from_dict(dict). In this last case dict should be something like dict = {a:[1,2,3], "b": ["a", "b", "c"], "c":...}.

see: Pandas Dataframe from dict with empty list value

pandas.DataFrame, 0: If data is a dict, argument order is maintained for Python 3.6 and later. index : Index or array-like. Index to use for resulting frame. Will default to RangeIndex if no  Returns dict, list or collections.abc.Mapping. Return a collections.abc.Mapping object representing the DataFrame. The resulting transformation depends on the orient parameter.

Pandas : How to create an empty DataFrame and append rows , Create an empty DataFrame with only column names but no rows print('*** Appends rows to an empty DataFrame using dictionary with  # Create an empty Dataframe with columns or indices dfObj = pd.DataFrame(columns=['User_ID', 'UserName', 'Action'], index=['a', 'b', 'c']) print("Empty Dataframe", dfObj, sep=' ') Here we passed the columns & index arguments to Dataframe constructor but without data argument. So, it will create an empty dataframe with all data as NaN.

Python Pandas : How to create DataFrame from dictionary , Create DataFrame from Dictionary using default Constructor Pass dictionary in Dataframe constructor to create a new object a dataframe · Pandas : How to create an empty DataFrame and append rows & columns to it in  Create a DataFrame from Dict of ndarrays / Lists All the ndarrays must be of same length. If index is passed, then the length of the index should equal to the length of the arrays. If no index is passed, then by default, index will be range (n), where n is the array length.

Using assign to place values from a dict into an empty dataframe , summary= pd.DataFrame() myData = dict() myData['B'] = 7 myData['C'] = -9 summary = summary.assign(**myData) The result of  Steps to Convert Dictionary to Pandas DataFrame Step 1: Gather the Data for the Dictionary. To start, gather the data for your dictionary. For example, I gathered the following data about products and prices: