Elnaz
Elnaz

Reputation: 1

query from a csv file

I want to draw a plot of people who are more than 0.5 years old. when I enter the data in python and make the data-frame, my code works:

import pandas as pd

data = {'age': [0.62,0.84,0.78,0.80,0.70,0.25,0.32,0.86,0.75],
'gender': [1,0,0,0,1,0,0,1,0],
'LOS': [0.11,0.37,0.23,-0.02,0.19,0.27,0.37,0.31,0.21],
'WBS': [9.42,4.40,6.80,9.30,5.30,5.90,3.10,4.10,12.07],
'HB': [22.44,10.40,15.60,15.10,11.30,10.60,12.50,10.40,14.10],
'Nothrophil': [70.43,88.40,76.50,87,82,87.59,15.40,77,88]}
df = pd.DataFrame(data, index=[0,1,2,3,4,5,6,7,8])

old = df.query('age > 0.5')

import matplotlib.pyplot as plt
plt.plot(old.age)
plt.show()

but when I use a csv file to form my data-frame, the code dosen’t work:

 import pandas as pd
df= pd.read_csv('F:\HCSE\sample_data1.csv',sep=';')
old = df.query('age > 0.5')
import matplotlib.pyplot as plt
plt.plot(old.age)
plt.show()

How can I use a csv file and do the same action?

and one more question. Is it possible to draw a scatter plot with only one argument?

As an example I want to draw a scatter plot of people who are more than 0.5 years old (Y axis is the age and the X axis is the number of datas or number of rows in csv file) and I want to use different colors for different genders. how can I do it?

Thanks a lot.

Upvotes: 0

Views: 47

Answers (2)

harpan
harpan

Reputation: 8631

but when I use a csv file to form my data-frame, the code dosen’t work:

You might want to share the error message so that we can know, what is going on under the hood.

Is it possible to draw a scatter plot with only one argument?

As an example I want to draw a scatter plot of people who are more than 0.5 years old (Y axis is the age and the X axis is the number of datas or number of rows in csv file) and I want to use different colors for different genders. how can I do it?

Yes. Please refer to below code.

colors = ['b' if gender == 1 else 'r' for gender in df.loc[df['age'] >0.5].gender]
df.loc[df['age'] > 0.5].reset_index().plot.scatter('index', 'age', color=colors)

enter image description here

You also can do this very easily using seaborn's lmplot.

import seaborn as sns

sns.lmplot(x="index", y="age", data=df.loc[df['age'] > 0.5].reset_index(), hue="gender", fit_reg=False)

enter image description here

Notice that you can apply colors according to gender with hue argument. Hope this helps for the visualization.

Upvotes: 1

herculanodavi
herculanodavi

Reputation: 228

For the scatter plot, you could simply do:

colors = ['b' if gender == 1 else 'r' for gender in old.gender]
plt.scatter(range(len(old.age)), old.age, color = colors)
plt.show()

About the query, can you put your .csv file? It works with my data.

Upvotes: 0

Related Questions