简体   繁体   中英

How do I access certain data from a table?

Just say I have a table of data for people, with the columns being Name, Age, and Height. However I want to show the relationship with only people named 'George', and 'Tom', with their ages on a scatterplot. This is the data example below:


import pandas as pd
import matplotlib.pyplot as plt

people = (['Tom', 18, 175], ['Steph', 24, 164], ['George', 23, 182],
          ['George', 29, 162], ['Tom', 17, 78], ['Nick', 21, 172.5], ['Harry', 22, 169])

df = pd.DataFrame(people, columns=['Name', 'Age', 'Height(cm)'])

So I want to plot just for the name Tom and George with their ages but I'm not too sure how to access certain elements of the table column. Sorry I'm new to Python and this might be a dumb and easy question, but here is what i've tried:


plt.scatter((df['Name':'Tom'], df['Name':'George']), df['Age'])

But obviously I don't think you can access it that way. Any help is appreciated!

If all you need to do is subsetting your dataframe by a specific value of one of the columns, you can do:

df.loc[df.Name=='Tom']

If you want to create a scatter plot of a subset of the data (in this case, for Tom and George), you can do something like this:

df.loc[df.Name.isin(['Tom', 'George'])].plot.scatter(x='Age', y='Height(cm)')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM