How to sort the value on plot?

image
the month is wrong in order. How can I sort to the right order ? the index of the table is wrong so the plot take wrong order too.

1 Like

This answer from Stack Overflow can help:

1 Like
t = pd.read_sql('select * from AATD_FC_EOM',con) 
t['RS_POS']=(t['TOTAL_RESOLVED_POS_BOM']/t['TOTAL_POS_BOM'])*100
fc5548.sort_values(by='MONTH', ascending=True)
fc5548 = t.loc[t['UNIT_CODE_FULL']=='FC5548']
fc5548=pd.series(data = fc5548['RS_POS'],index=['2018/08','2018/09','2018/10','2018/11','2018/12','2019/01','2019/02','2019/03','2019/04','2019/05','2019/06','2019/07','2019/08'])
fig=plt.figure(figsize=(7,5))
fc5548.plot(use_index=False)
plt.xticks(rotation=90)

plt.show()
1 Like

I do not understand the pd.series and the code I wrote saying that “module ‘pandas’ has no attribute ‘series’”

I have edited your code block to use back ticks ```. You can click edit to see how I edit them.

1 Like

You have to assign fc5548 to fc5548.sort_values. Otherwise fc5548 is not sorted.

fc5548 = fc5548.sort_values(by='MONTH', ascending=True)

If you want to do it in-place (the data frame will get the new sorted value), then

fc5548.sort_values(by='MONTH', ascending=True, inplace=True)

It should be in the correct order now.

You can read more about .sort_values from the documentation.

All other pandas method returns a new dataframe (since by default inplace=False).

1 Like

C:\Program Files (x86)\Anaconda3\lib\site-packages\ipykernel_launcher.py:4: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
after removing the cwd from sys.path.

It is saying the error above

1 Like

Here is an excellent blog post that breaks down what happens with this warning: https://www.dataquest.io/blog/settingwithcopywarning/

You should repost your code and indicate which line does the code cause the error.

2 Likes

Thank you ^^ I dit it. Today, I have learned the new things about sort_values

1 Like

You are welcome. How did you resolve the settingwithcopyWarning?

Sorry, sort_values does not solve the problem
Actually, I have added the order by SQL statement to modify the table for month .
t = pd.read_sql('select * from AATD_FC_EOM order by month',con)

1 Like
fc5548 = fc5548.sort_values(by='MONTH', ascending=True, inplace=True) 

inplace=True we are doing inplace replacement. There is no need to assign fc5548.

Replace to the following:

fc5548.sort_values(by='MONTH', ascending=True, inplace=True) 
1 Like

Ok, It works ! No need to write Order by SQL statement ! Love DQ !

1 Like

That’s good! :smiling_face_with_three_hearts:

Glad that it works.

1 Like