I have a Pandas DataFrame that looks as shown
.
The column localhour is the index. My question is how can I use plot scatter to scatter the localhour column (the index) with use column. Thanks in advance.
Related
My pandas dataframe is like this
name,id,score1,score2,score3
ax,1,32,43,32
sy,1,34,22,22
ax,2,22,6,8
ax,3,10,5,4
bz,1,22,2,2
sy,2,10,1,0
bz,2,9,8,1
How can I create a line plot in seaborn for each unique name where id would be the x-axis, and the y-axis would be value, and the hue would be 'score1', 'score2', and 'score3'? Each unique name will have a separate line plot.
sns.lineplot(data=flights, x="year", y="passengers", hue="month")
I wonder how do u handle the scenario where the hue is on different columns on a pandas dataframe? In my case, that is 'score1', 'score2', score3'
Thanks!
What you want is unclear, but you can always melt your data before plotting:
import seaborn as sns
sns.lineplot(data=df.melt(['name', 'id'], var_name='score', value_name='value'),
x='id', y='value', style='name', hue='score')
Output:
I have been playing with aggregation in pandas dataframe. Considering the following dataframe:
df=pd.DataFrame({'a':[1,2,3,4,5,6,7,8],
'batch':['q','q','q','w','w','w','w','e'],
'c':[4,1,3,4,5,1,3,2]})
I have to do aggregation on the batch column with mean for column a and min for column c.
I used the following method to do the aggregation:
agg_dict = {'a':{'a':'mean'},'c':{'c':'min'}}
aggregated_df = df.groupby("batch").agg(agg_dict)
The problem is that I want the final data frame to have the same columns as the original data frame with the slight difference of having the aggregated values present in each of the columns.
The result of the above aggregation is a multi-index data frame, and am not sure how to convert it to an individual data frame?
I followed the link: Reverting from multiindex to single index dataframe in pandas . But, this didn't work, and the final output was still a multi-index data frame.
Great, if someone could help
you can try the following code df.groupby('batch').aggregate({'c':'min','a':mean})
So I have a dataframe say df, with multiple columns. I now create a dataframe from df, say map, passing only columns A and B and keeping only the unique rows. Now I want to modify df in such a way such that, if for a row in df, I find df['B'] in the map, then df['A'] should be key value from the map otherwise df['A' remains the same.
Any useful suggestions would be appreciated.
Using ggplot2 I want to plot a barplot using colors included in the column of that dataframe
Reproducible example below:
df<-cbind.data.frame("bar_colors"=c("red","blue","green"), "Counts"=c(10,20,30), "Spp"=c("a","a","a"))
ggplot(data=df, aes(x=Spp,y=as.numeric(as.character(Counts)), fill=bar_colors)) +
geom_bar(stat="identity")+scale_fill_manual(values = as.character(df$bar_colors))
But the colors are mixed-up. What do I miss?
Thanks
EDIT:
Solved myself by releveling the factor:
df$bar_colors<-factor(df$bar_colors, levels = as.character(df$bar_colors))
How can I create a DataFrame with Julia with index names that are different from Row and write values into a (index,column) pair?
I do the following in Python with pandas:
import pandas as pd
df = pd.DataFrame(index = ['Maria', 'John'], columns = ['consumption','age'])
df.loc['Maria']['age'] = 52
I would like to do the same in Julia. How can I do this? The documentation shows a DataFrame similar to the one I would like to construct but I cannot figure out how.