Home > Enterprise >  why groupby change the rows number
why groupby change the rows number

Time:10-16

i have this code that i try to plot column x Ct and Fs based on Ft so i tried groupby but what i noticed that before using group by the there were 400 rows and after using groupby it becomes 257 rows and only with the Ft="C" it doesn't show the other values which are A and B

so how can i solve this?

df = pd.read_csv('f.txt',sep=" ",names=list(["Ct", "Fs", "Ft"]))
df.iloc[:]




groups = df.groupby("Ft")
plt.subplots(figsize=(18,10))
for name, group in groups:

    plt.scatter( group.Ct,group.Fs, label=name,s=100)

plt.yticks(np.arange(0, 6,0.5))
plt.xticks(np.arange(0, 24,1))
plt.title('f',fontsize=20)
plt.xlabel('x',fontsize=20)
plt.ylabel('y',fontsize=20)

plt.legend(loc='upper center', ncol=3)

group.iloc[:]

this is before groupby

this is after groupby

CodePudding user response:

It doesn't automatically change the index names, that's the behavior of groupby, just reset the index then:

groups = groups.agg('first').reset_index(drop=True)

CodePudding user response:

If you are trying to make a scatter plot of Ct and Fs and want to have each point colored based on Ft I suggest using Seaborn or Plotly. Matplotlib takes a bit more work to color by an object column.

No groupby needed.

After installing those libraries, here's how you do it.

import seaborn as sns
sns.scatterplot(data=df, x='Ct', y='Fs', hue='Ft')

or

import plotly.express as px

px.scatter(data=df, x='Ct', y='Fs', color='Ft')

  • Related