python pandas Dataframe with User level inputs

Viewed 1700

I'm Just started learning pandas where i am using Dataframe and data structure for learning , So when i am providing hardcode values for Dictionary and Index the it works fine but i want this to be user input based where user can put the inputs and those vaue can be stored on the Dictionary and based on that it can produce the expected results:

The below example code runs good with hardcode values within Dictionary & Index ..

import pandas as pd
import numpy as np
###########  computation by numpy vectorisation method #######
purchae_1 = pd.Series({'Name': 'Karn',
                       'Item Purchased': 'Dog Food',
                       'Cost': 22.50})

purchae_2 = pd.Series({'Name': 'Renu',
                       'Item Purchased': 'Kitty Letter',
                       'Cost': 2.50})

purchae_3 = pd.Series({'Name': 'Rigved',
                       'Item Purchased': 'Foot Ball',
                       'Cost': 12.50})

#df = pd.DataFrame([purchae_1,purchae_2,purchae_3], index = ['Store1', 'Store2', 'Store3'])
df = pd.DataFrame([purchae_1,purchae_2,purchae_3], index = ['Store1', 'Store2', 'Store3'])
print(df.head())

   bash-4.1$ ./pythonDatafram.py
            Cost Item Purchased    Name
    Store1  22.5       Dog Food    Karn
    Store2   2.5   Kitty Letter    Renu
    Store3  12.5      Foot Ball  Rigved

While in The below example i'm trying to build it in such a way so, it will ask for User's input and based on that Dtaframe will be created and result can be yeilded but someone its not working properly

import pandas as pd
import numpy as np

User_Name  = input('Name ')
Item_Purchased = input('Item Purchased ')
Item_Cost = input('Cost ')

purchae_1 = pd.Series( {'Name ': User_Name,
                        'Item_Purchased ' : Item_Purchased,
                        'Item_Cost ' : Item_Cost})

purchae_2 = pd.Series({'Name ': User_Name,
                       'Item Purchased ': Item_Purchased,
                       'Cost ': Item_Cost})

purchae_3 = pd.Series({'Name ': User_Name,
                       'Item Purchased ': Item_Purchased,
                       'Cost ': Item_Cost})

df = pd.DataFrame([purchae_1,purchae_2,purchae_3], index = ['Store1', 'Store2', 'Store3'])
print(df.head())

So, when i executed it , it shows the below results.. please help me to understand what i need to do to make it running for other sequese as well.. as i have defined the Variable purchase_1, purchase_2 & purchase_3 where it only pics First one and skips the rest...

bash-4.1$ ./pythonDatafram.py
Name Karn
Item Purchased Dog Food
Cost 22.50
        Cost  Item Purchased  Item_Cost  Item_Purchased  Name
Store1    NaN             NaN      22.50        Dog Food  Karn
Store2  22.50        Dog Food        NaN             NaN  Karn
Store3  22.50        Dog Food        NaN             NaN  Karn
bash-4.1$
2 Answers

in the last example you give, there are different column names in purchae_1. You use Cost twice. and Item_Cost once. In purchae_1 change Item_Cost to Cost and Item_Purchased to Item Purchased In essence, this issue is happening because the column names are different. A very simple fix!

import pandas as pd
import numpy as np

User_Name  = input('Name ')
Item_Purchased = input('Item Purchased ')
Item_Cost = input('Cost ')

purchae_1 = pd.Series( {'Name ': User_Name,
                        'Item Purchased ' : Item_Purchased, #<--- change is here
                        'Cost ' : Item_Cost}) #<--- change is here

purchae_2 = pd.Series({'Name ': User_Name,
                       'Item Purchased ': Item_Purchased,
                       'Cost ': Item_Cost})

purchae_3 = pd.Series({'Name ': User_Name,
                       'Item Purchased ': Item_Purchased,
                       'Cost ': Item_Cost})

df = pd.DataFrame([purchae_1,purchae_2,purchae_3], index = ['Store1', 'Store2', 'Store3'])
print(df.head())

i think that you problem comes from mislabels. Sometimes you use 'Cost ' and other times 'Item_Cost'; same thing for 'Item_Purchased ' and 'Item Purchased '

if you keep using same labels it should work:

purchae_1 = pd.Series( {'Name ': User_Name,
                    'Item Purchased ' : Item_Purchased,
                    'Cost ' : Item_Cost})

purchae_2 = pd.Series({'Name ': User_Name,
                   'Item Purchased ': Item_Purchased,
                   'Cost ': Item_Cost})

purchae_3 = pd.Series({'Name ': User_Name,
                   'Item Purchased ': Item_Purchased,
                   'Cost ': Item_Cost})
Related