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" : '[email protected]', "phone_number" : 55667788, "notes" : 'hei'}
user_df = pd.DataFrame(credentials['username'], index=[0])
print(user_df)
Output:
password email phone_number notes 0 123 [email protected] 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" : '[email protected]', "phone_number" : 55667788, "notes" : 'hei'}
user_df = pd.DataFrame(credentials['username'], index=[0])
print(user_df)
Output:
password email phone_number notes 0 123 [email protected] 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
  for loop in dataframe in pandas Paulman 7 207 Dec-02-2021, 12:15 AM
Last Post: bowlofred
  Structuring and pivoting corrupted dataframe in pandas gunner1905 2 406 Sep-18-2021, 01:30 PM
Last Post: gunner1905
  TypeError: 'DataFrame' object is not callable using Pandas in Python sofiavlachou 1 1,404 Sep-02-2021, 03:24 PM
Last Post: buran
  Problem in saving .xlsm (excel) file using pandas dataframe in python shantanu97 2 689 Aug-29-2021, 12:39 PM
Last Post: snippsat
  Partial Matching Rows In Pandas DataFrame Query eddywinch82 1 610 Jul-08-2021, 06:32 PM
Last Post: eddywinch82
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 699 May-04-2021, 10:51 PM
Last Post: rhat398
Star Split and organize my Pandas Dataframe brunolelli 4 1,011 Apr-18-2021, 03:00 AM
Last Post: brunolelli
  Convert MultiLayer XML to DataFrame using Pandas vsingh17 0 862 Apr-14-2021, 03:50 PM
Last Post: vsingh17
  Pandas DataFrame Code Query eddywinch82 6 1,427 Feb-12-2021, 09:55 PM
Last Post: eddywinch82
  Pandas dataframe without index tgottsc1 3 6,868 Feb-01-2021, 05:29 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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