I have the following data frame:
year tradevalueus partner
0 1989 26065 Algeria
1 1989 12345 Albania
2 1991 178144 Argentina
3 1991 44384 Bhutan
4 1990 1756844 Bulgaria
5 1990 57088556 Myanmar
I want a bar graph by year on the x-axis for each trade partner with values. By this, with the above data, I want to have 3 years on the x-axis with 2 bar-graphs for each year with the tradevalueus variable and I want to name each of these by the partner column. I have checked df.plot.bar() and other stackoverflow posts about bar graphs but they don't give the output I desire. Any pointers would be greatly appreciated.
Thanks!
You can either pivot the table and plot:
df.pivot(index='year',columns='partner',values='tradevalueus').plot.bar()
Or use seaborn:
import seaborn as sns
sns.barplot(x='year', y='tradevalueus', hue='partner', data=df, dodge=True)
Output:
I have the next dataframe in pandas-
Perpetrator Perpetrator Gender
Age Sex
1 2 Female
2 2 Female
3 3 Female
4 5 Female
5 7 Female
6 7 Female
7 7 Female...
where:
Perpetrator Age means the age of the Perpetrator
Gender means the perpetrator gender and
Perpetrator Sex mean the amount of perpetrators of that gender
for example - there are 5 female perpetrators that are 4 years old.
I am trying to make a seaborn bar chart that has two sides (columns) - one for female and one for male, and see the amounts of each age.
tried using-
g = sns.catplot(x="Perpetrator Age", y="Perpetrator Sex",col="Gender",
data=final_df5, saturation=.5,
kind="bar")
and
sns.displot(penguins, x="flipper_length_mm", col="sex", multiple="dodge")
(from here )
but nothing seems to work.
I keep getting this error -
ValueError: Could not interpret input 'Perpetrator Age'
Thank you
What do you get when you try:
print(df.columns)
You want it to look like:
Index(['Perpetrator Age', 'Perpetrator Sex', 'Gender'], dtype='object')
But, it looks like you may have heirarchical-indexed data. If you don't and it looks like above, you can try this seaborn plotting code:
import seaborn as sns
g = sns.catplot(x='Perpetrator Age', y="Perpetrator Sex", hue="Gender",
data=df,saturation=.5, dodge=True, ci=None,kind="bar")
You need to change the col= to hue= in your code, and set dodge=True.
Result from random data.:
EDIT
It looks like your dataframe's index is the Perpetrator's Age. To solve your issue reset the index and then plot (this time the code plot's the genders in two separate plots):
final_df5. reset_index(inplace=True)
import seaborn as sns
g = sns.catplot(x='Perpetrator Age', y="Perpetrator Sex",
col='Gender', color='blue',
data=final_df5, dodge=True,
ci=None, kind="bar")
Result:
I would like to find a shortcut to labeling data since I am working with a large data set.
here's the data I'm charting from the large data set:
Nationality
Afghanistan 4
Albania 40
Algeria 60
Andorra 1
Angola 15
...
Uzbekistan 2
Venezuela 67
Wales 129
Zambia 9
Zimbabwe 13
Name: count, Length: 164, dtype: int64
And so far this is my code:
import pandas as pd
import matplotlib.pyplot as plt
the_data = pd.read_csv('fifa_data.csv')
plt.title('Percentage of Players from Each Country')
the_data['count'] = 1
Nations = the_data.groupby(['Nationality']).count()['count']
plt.pie(Nations)
plt.show()
creating the pie chart is easy and quick this way but I haven't figured out how to automatically label each country in the pie chart without having to label each data point one by one.
pandas plot function would automatic label the data for you
# count:
Nations = the_data.groupby('Nationality').size()
# plot data
Nations.plot.pie()
plt.title('Percentage of Players from Each Country')
plt.show()
I have a pandas dataframe which looks like this:
Country
Japan
Japan
Korea
India
India
USA
USA
USA
I need to count the unique values of the country column and change to percentage and need to put in the x-axis and y-axis of plotly bar chart. Can anyone teach me how to do it?
Use value_counts:
df.Country.value_counts(normalize=True)
I have a pandas dataframe which looks like this:
Country Sold
Japan 3432
Japan 4364
Korea 2231
India 1130
India 2342
USA 4333
USA 2356
USA 3423
I want to plot graphs using this dataframe. The plot will have country names on X-axis and the mean/sum of the sold of each country will on y-axis. I know I can compute the mean/sum using the group by function like this:
df.groupby('Country')['Sold'].mean()
I know the plotly histogram has the function that can directly compute the value and plot the graph. But I want to apply on other graph of plotly such as bar chart to make the graph more interactive. I am having a hard time figuring it out. Can anyone please help me out?