Home > Blockchain >  Printing out data from a row with user input with pandas python
Printing out data from a row with user input with pandas python

Time:10-19

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:\Users\VivoBook\Genshin_Stats\main.py", line 11, in <module>
    print(df[df.charecter==charcter])
  File "C:\Users\VivoBook\Genshin_Stats\venv\lib\site-packages\pandas\core\generic.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?

CodePudding user response:

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")

CodePudding user response:

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!

CodePudding user response:

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.

  • Related