Mike Jones Mike Jones - 1 year ago 59
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()

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.


It will display all the columns matching that substring of yaxis

here is an example.

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

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

df = pd.DataFrame(A)

   Age    Name
0   20    John
1   23  Andrew
2   42   Smith

0    John
1  Andrew
2   Smith

   Age    Name
0   20    John
1   23  Andrew
2   42   Smith
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download