csv - Python pandas trouble with storing result in variable -
i'm using pandas handle csv file, i'm having trouble storing results in variable , printing out is.
this code have.
df = pd.read_csv(my_file.csv, index_col=false, header=0) df2 = df[(df['name'])] # trying result of name variable n = df2['name'] print(n)
and result get:
1 jake name: name, dtype: object
my question:
is possible have "jake" stored in variable "n" can call out whenever need it?
eg: print (n)
result: jake
this code have constructed
def name_search(): list_to_open = input("which list open: ") + ".csv" directory = "c:\users\jake wong\pycharmprojects\box" "\\" + list_to_open if os.path.isfile(directory): # search name name_id = input("name search for: ") df = pd.read_csv(directory, index_col=false, header=0) df2 = df[(df['name'] == name_id)] # defining name save file n = df2['name'].ix[1] print(n)
this in csv file
s/n,name,points,test1,test2,test3 s49,sing chun,5000,sc,90 sunrsie,4984365132 s49,alice suh,5000,jake,88 sunrsie,15641816 s1231,alice suhfds,5000,sw,54290 sunrsie,1561986153 s49,jake wong,5000,jake,88 sunrsie,15641816
the problem n = df2['name']
pandas series:
type(df.loc[df.name == 'jake wong'].name) pandas.core.series.series
if want value, can use values[0]
-- values underlying array behind pandas object, , in case it's length 1, , you're taking first element.
n = df2['name'].values[0]
also csv not formatted properly: it's not enough have things lined in columns that, need have consistent delimiter (a comma or tab usually) between columns, parser can know when 1 column ends , 1 starts. can fix csv this?:
s/n,name,points s56,alice suh,5000 s49,jake wong,5000
otherwise can work on solution use regex rather pandas.
Comments
Post a Comment