Printing out data from a row with user input with pandas python

Question:

import pandas as pd
df=pd.read_csv('C:/Users/VivoBook/Genshin_Stats/Genshin_stats2.csv', header=0,
                      names=['Charcter ','Vision','Weapon','Best Weapon','Best weapon (F2P)','Other Weapon','Role',
                                   'Reccomended artifact set','Reccomended artifact set 2'])

df.set_index('Charcter ', inplace= True)
print(df.index)
while True:
       Charecter = input('charecter name: ')
       if Charecter in df.index:
          print(df[df.charecter==charcter])
       elif 'Charcter ' == '':
              break
       else:
           print('Charcter ',"not found")

I am basically making a program using python pandas where by entering the name of a charecter you get information about them such as their weapon, abilities etc.
This is my code however, when I run it the following error is produced.

charecter name: Albedo

Traceback (most recent call last):
  File "C:UsersVivoBookGenshin_Statsmain.py", line 11, in <module>
    print(df[df.charecter==charcter])
  File "C:UsersVivoBookGenshin_Statsvenvlibsite-packagespandascoregeneric.py", line 5907, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'charecter'

Im new to coding and I dont understand the error that i am making could someone please help me?

Asked By: Kitsune Udon

||

Answers:

Looks like a typo here. Instead of

print(df[df.charecter==charcter])

do:

print(df[df.charcter==charecter])

charcter is the attribute here not charecter!

Answered By: Bishwash Karki

Hello and welcome to the python community. You are simply making some typing errors. Instead of print(df[df.charecter==charcter]) try print(df[df.Charcter==charcter]).

Note that the right spelling of the word is character.

Answered By: Niklas B

try this:

import pandas as pd
df=pd.read_csv('C:/Users/VivoBook/Genshin_Stats/Genshin_stats2.csv', header=0,
                      names=['Charcter ','Vision','Weapon','Best Weapon','Best weapon (F2P)','Other Weapon','Role',
                                   'Reccomended artifact set','Reccomended artifact set 2'])

df.set_index('Charcter ', inplace= True)
print(df.index)
while True:
       Charecter = input('charecter name: ')
       if Charecter in df.index:
          print(df[df.index==Charecter]) # ---> The column you are trying to print no longer exists because we have set it to index so you have to call it as "df.index"
       elif 'Charcter ' == '':
              break
       else:
           print('Charcter ',"not found")
Answered By: Clegane
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.