jprothro
jprothro

Reputation: 1

How to use .loc function to collect data from certain rows

I am trying to plot data from specific rows of a .csv file based on the input of the user. The first column of the data file is a unique ID value and the following three columns are the x,y,z values to be plotted.

Once the user inputs one of the ID values, how do I only plot the x, y, z data of all of the rows with the same ID value as the input ID value?

I have been trying to use .loc but have not had any success.

input_id = int(input())
df = pd.read_csv('filename.csv')
df = df.loc[df['id column'] == input_id]

Example of the data

Upvotes: 0

Views: 1609

Answers (2)

Michael Bridges
Michael Bridges

Reputation: 391

Use a condition/mask and .loc to select only the rows you want, then drop the rest, and finally plot the dataframe.

condition = (df['id column'] == input_id)
df = df.loc[condition].dropna()
df.plot(kind='line',x='index',y='value')
plt.show()

Upvotes: 1

Kenan
Kenan

Reputation: 14104

Call plot of your df

import matplotlib.pyplot as plt

df = df.loc[df['id column'] == input_id]
df[['columns_to_plot']].plot() # assuming it's x, y ,z
plt.show()

Check the doc for plotting types: dataframe.plot

Upvotes: 2

Related Questions