Mike Jones Mike Jones - 1 year ago 42
Python Question

Asking user to plot two columns in a csv file without him typing the entire column name?

My current code:



import pandas as pd

df = pd.DataFrame.from_csv('data.csv',index_col=False)


while True:
print (df)
print ('Select X-Axis')
xaxis = input()
print ('Select Y-Axis')
yaxis = input()
break

df.plot(x= xaxis, y= yaxis)





My current works only if user types out the entire column name. However, some of the columns are large. I want to make it easier for my user so that if he types out part of the column name it works. Or if somehow I can assign each column name with a number without manually going through each column and assigning a number. Then I can show a list to the user and he types out the number he would like to plot.

Answer Source

One way to do is to use filter in pandas for that purpose.

df.filter(regex=(yaxis))

It will display all the columns matching that substring of yaxis

here is an example.

A = { 'Name': [ 'John', 'Andrew', 'Smith'] , 'Age' : [20,23,42]}

A
Out[19]: {'Age': [20, 23, 42], 'Name': ['John', 'Andrew', 'Smith']}

df = pd.DataFrame(A)

df
Out[21]: 
   Age    Name
0   20    John
1   23  Andrew
2   42   Smith

df.filter(regex=('Na'))
Out[22]: 
     Name
0    John
1  Andrew
2   Smith

df.filter(regex=('e'))
Out[23]: 
   Age    Name
0   20    John
1   23  Andrew
2   42   Smith