Skip to content Skip to sidebar Skip to footer

Is There Any Way To Remove Column And Rows Numbers From Dataframe.from_dict?

So, I have a problem with my dataframe from dictionary - python actually 'names' my rows and columns with numbers. Here's my code: a = dict() dfList = [x for x in df['Marka'].tolis

Solution 1:

By sorting the dict_items object (a.items()), you have created a list. You can check this with type(sorted_by_value). Then, when you try to use the pd.DataFrame.from_dict() method, it fails because it is expecting a dictionary, which has 'values', but instead receives a list.

Probably the smallest fix you can make to the code is to replace the line:

dataframe=pd.DataFrame.from_dict(sorted_by_value)

with:

dataframe = pd.DataFrame(dict(sorted_by_value), index=[0]).

(The index=[0] argument is required here because pd.DataFrame expects a dictionary to be in the form {'key1': [list1, of, values], 'key2': [list2, of, values]} but instead sorted_by_value is converted to the form {'key1': value1, 'key2': value2}.)

Another option is to use pd.DataFrame(sorted_by_value) to generate a dataframe directly from the sorted items, although you may need to tweak sorted_by_value or the result to get the desired dataframe format.

Alternatively, look at collections.OrderedDict (the documentation for which is here) to avoid sorting to a list and then converting back to a dictionary.

Edit

Regarding naming of columns and the index, without seeing the data/desired result it's difficult to give specific advice. The options above will allow remove the error and allow you to create a dataframe, the columns of which can then be renamed using dataframe.columns = [list, of, column, headings]. For the index, look at pd.DataFrame.set_index(drop=True) (docs) and pd.DataFrame.reset_index() (docs).

Solution 2:

index and columns are properties of your dataframe

As long as len(df.index) > 0 and len(df.columns) > 0, i.e. your dataframe has nonzero rows and nonzero columns, you cannot get rid of the labels from your pd.DataFrame object. Whether the dataframe is constructed from a dictionary, or otherwise, is irrelevant.

What you can do is remove them from a representation of your dataframe, with output either as a Python str object or a CSV file. Here's a minimal example:

df = pd.DataFrame([[1, 2, 3], [4, 5, 6]])

print(df)
#    0  1  2# 0  1  2  3# 1  4  5  6# output to string without index or headersprint(df.to_string(index=False, header=False))
# 1  2  3# 4  5  6# output to csv without index or headers
df.to_csv('file.csv', index=False, header=False)

Post a Comment for "Is There Any Way To Remove Column And Rows Numbers From Dataframe.from_dict?"