Python Forum
Pandas dictionary dataframe help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas dictionary dataframe help
#1
I am developing a little program that stores user credentials in a dictionary and transfer them in a Pandas dataframe. Since the credentials are stored in a nested dictionary that begins with the username entered by the user, i dont know how to index the nested dictionary. Returns this error:
Error:
Traceback (most recent call last): File "profile_creator.py", line 29, in <module> add_dict(username,password,email,phone_number,notes) File "profile_creator.py", line 6, in add_dict user_df = pd.DataFrame(credentials[0]) KeyError: 0
import pandas as pd

def add_dict(username,password,email,phone_number,notes):
    credentials = dict()
    credentials[username] = {"password" : password, "email" : email, "phone_number" : phone_number, "notes" : notes}    
    user_df = pd.DataFrame(credentials[0])
    print(user_df)
Reply
#2
Post code in code tag not image,same as you dos with error message.
Try change to.
user_df = pd.DataFrame(credentials['username'], index=[0])
Edit:
Ok i see you change it.
Reply
#3
(Jun-18-2021, 08:24 PM)snippsat Wrote: Post code in code tag not image,same as you dos with error message.
Try change to.
user_df = pd.DataFrame(credentials['username'], index=[0])
Edit:
Ok i see you change it.

I can't know what does the user enter as username because it is stored into a variable.
Error:
Traceback (most recent call last): File "profile_creator.py", line 29, in <module> add_dict(username,password,email,phone_number,notes) File "profile_creator.py", line 6, in add_dict user_df = pd.DataFrame(credentials['username'], index=[0]) KeyError: 'username'
Reply
#4
Try this,i see your username in not just a string but a variable that get as argument from function.
user_df = pd.DataFrame(credentials[username], index=[0])
Basic test to see if this work.
import pandas as pd

credentials = dict()
credentials['username'] = {"password" : 123, "email" : 'test@org', "phone_number" : 55667788, "notes" : 'hei'}
user_df = pd.DataFrame(credentials['username'], index=[0])
print(user_df)
Output:
password email phone_number notes 0 123 test@org 55667788 hei
Reply
#5
(Jun-18-2021, 09:16 PM)snippsat Wrote: Try this,i see your username in not just a string but a variable that get as argument from function.
user_df = pd.DataFrame(credentials[username], index=[0])
Basic test to see if this work.
import pandas as pd

credentials = dict()
credentials['username'] = {"password" : 123, "email" : 'test@org', "phone_number" : 55667788, "notes" : 'hei'}
user_df = pd.DataFrame(credentials['username'], index=[0])
print(user_df)
Output:
password email phone_number notes 0 123 test@org 55667788 hei

It does work but basically is not what I want to do. The dictionary key should be the same string that the user entered and in order to do that I use a variable, but once it is stored I don't know how to refer to it and so access it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 686 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Question on pandas.dataframe merging two colums shomikc 4 782 Jun-29-2023, 11:30 AM
Last Post: snippsat
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,297 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  help how to get size of pandas dataframe into MB\GB mg24 1 2,230 Jan-28-2023, 01:23 PM
Last Post: snippsat
  pandas dataframe into csv .... exponent issue mg24 10 1,706 Jan-20-2023, 08:15 PM
Last Post: deanhystad
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 797 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  How to retrieve records in a DataFrame (Python/Pandas) that contains leading or trail mmunozjr 3 1,694 Sep-05-2022, 11:56 AM
Last Post: Pedroski55
  "Vlookup" in pandas dataframe doug2019 3 1,796 May-09-2022, 01:35 PM
Last Post: snippsat
  Increase the speed of a python loop over a pandas dataframe mcva 0 1,290 Jan-21-2022, 06:24 PM
Last Post: mcva
  for loop in dataframe in pandas Paulman 7 2,672 Dec-02-2021, 12:15 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020