How can I convert dictionary to dataframe in pandas? - python

I have a dictionary like this.
The inside of dictionary is ...
When I clicked the {top:'Dataframe',rising:'Dataframe'}, i accessed to two different Dataframes which are top and rising.
My question is that How can i access these dataframes directly?
I tried to use dict to dataframe examples. They did not work. Any help would be appreciated.

Use dictionary access operator [] twice
related_queries_dict['Big Data']['rising']

Related

Is there a way to extract contents from a JSON list inside a pandas dataframe cell?

I have a column in a pandas dataframe that contains JSONs just like the example below. I want to extract just the zipcode value from either of the banks, but I can't.
[{"Bank1":{"zipcode":"045603", "total_amount":"400000"}}, {"Bank2":{"{"zipcode":"07069", "total_amount":"890000"}}]
I tried doing the following, which would work if it wasn't a JSON list, I believe:
df['bank1_zipcode'] = df['bank_data'].str['Bank1'].str['zipcode']
But had no success.
Thanks in advance!!!
I think you can use map
df['bank_data'].map(lambda x: x['Bank1']['zipcode'])
It's a list, try
df['bank1_zipcode'] = df['bank_data'].str[0].str['Bank1'].str['zipcode']

Combine a single dataframe(left) with multiple dataframes within a list (right)

I have a list of dataframes. each of which holds 2 columns 'Key and Value' and then I have a main dataframe that holds all the keys that exist in the dataframes in the list.
I wanted to left join all of the dfs in the list with the main dataframe without using a long syntax.
df_main.merge(df_list[0], on='key_0', how='left').merge(df_list[1], on='key_1',
how='left').merge(df_list[2], on='key_2', how='left').....
This approach would be fine with a couple of tables in the list but not scalable.
Any help would be appreciated.
Thank you.

Create dataframes from a dictionary of names and dataframes

Tried my best looking for a similar answer, but didn't seem to find the necessary one.
I have a dictionary of dataframe objects, where the key is the dataframe name, and the value is the actual dataframe
table_names_dict = {'name_1': dataframe_1, 'name_2': dataframe_2}
I am trying to loop over the dictionary and dynamically create separate dataframes, using the keys as their names:
name_1 = dataframe_1
name_2 = dataframe_2
I tried something of the sort
for key, value in table_names_dict.items():
key = value
This simply created one dataframe named value
I've also tried
locals().update(table_names_dict)
Which did create the necessary variables, but they are not accessible in Spyders variable explorer, and from what I've read, the use of locals() is frowned upon.
What am I doing wrong?
You can use globals() for this:
for i in table_names_dict:
globals()[i]=table_names_dict[i]

create dynamic column names in pandas

I am trying to create multiple dataframes inside a for loop using the below code:
for i in range(len(columns)):
f'df_v{i+1}' = df.pivot(index="no", columns=list1[i], values=list2[i])
But I get the error "Cannot assign to literal". Not sure whether there is a way to create the dataframes dynamically in pandas?
This syntax
f'df_v{i+1}' = df.pivot(index="no", columns=list1[i], values=list2[i])
means that you are trying to assign DataFrames to a string, which is not possible. You might try using a dictionary, instead:
my_dfs = {}
for i in range(len(columns)):
my_dfs[f'df_v{i+1}'] = df.pivot(index="no", columns=list1[i], values=list2[i])
Since it allows the use of named keys, which seems like what you want. This way you can access your dataframes using my_dfs['df_v1'], for example.

how do I create a new column out of a dictionary's sub string on a pandas dataframe

I have the following repo for the files: https://github.com/Glarez/learning.git
dataframe
I need to create a column with the bold part of that string under the params column: "ufield_18":"ONLY" I dont see how can I get that since I'm learning to code from scratch. The solution to this would be nice, but what I would really appreciate is you to point me at the right direction to get the answer for myself. THANKS!
Since you do not want the exact answer. I will provide you one of the ways to achieve this:
filter the params column into a dictionary variable
create a loop to access the keys of the dictionary
append it to the pandas df you have (df[key] = np.nan) - Make sure you add some values while appending the column if your df already has some rows or just add np.nan
note np is numpy library which needs to be imported

Categories