Home > Net >  Plot number of persons in each car
Plot number of persons in each car

Time:07-07

I have a pandas dataframe which looks like this:

car,id
1,1
1,2
2,3
2,4
2,5
and so on

What I want to do is make a lineplot in seaborn that shows how many ids there are in each car ( I dont care for which id that are in the car). So on the x axis I want to have the unique number of cars (so here [1,2]) and y-axis I want the "number" of cars that are repeated (so here [2,3]). I would like to use seaborn to plot. What I have tried now is:

import seaborn as sns
#the df is the one above
sns.lineplot(x='car', y='car'.count(), data=df) #which is not working for obvious reasons

Any tips to do this?

CodePudding user response:

If you specifically need a lineplot then this would work:

import pandas as pd
import seaborn as sns

data = {"car": [1, 1, 2, 2, 2], "id": [1, 2, 3, 4, 5]}
df = pd.DataFrame(data)
sns.lineplot(x="car", y="id", data=df.groupby('car').nunique())

Or could use value_counts() too:

car_count = df['car'].value_counts()
sns.lineplot(x=car_count.index, y=car_count.values)

CodePudding user response:

import pandas as pd

MyDic = {"car": [1, 1, 2, 2, 2, 3], "id": [1, 2, 3, 4, 5, 6]}
MyDf = pd.DataFrame(MyDic)

print(MyDf)
>>    car  id
>> 0    1   1
>> 1    1   2
>> 2    2   3
>> 3    2   4
>> 4    2   5
>> 5    3   6

from collections import Counter

carCounter = Counter(MyDf["car"])
x, y = list(carCounter.keys()), list(carCounter.values())

print(f"{x=}, {y=}")
>>x=[1, 2, 3], y=[2, 3, 1]

CodePudding user response:

Line plot in a seaborn needs both axis. The below code will run fine.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

cars = {"id": [1, 2, 3, 4, 5, 6], "car": [1, 1, 2, 2, 2, 3]}
dataset = pd.DataFrame(cars)

car_counts = dataset["car"].value_counts()
car_counts.sort_index(inplace=True)

sns.lineplot(x=car_counts.index, y=car_counts)
plt.show()
  • Related