0

I want to have an option in the table in my site that allows the user to rearrange the table by min or max value in a specific column, but couldn't find anything about this.

The dataset I am using is this: https://raw.githubusercontent.com/plotly/datasets/master/bubble_chart_tutorial.csv

I want the GDP per capita column to be rearranged.

Tried using df.groupby(['country'], as_index=False)['GDP per capita'].max()

df = pd.read_csv(DBPath).rename_axis('id', axis = 1)
db_table = df.to_html(escape=False)


fig1, ax1 = plt.subplots()
plt.ylabel('Amount of Countries')
plt.xlabel('Life Expectancy')
df['life expectancy'].hist()

fig2, ax2 = plt.subplots()
plt.ylabel('Amount of Countires')
plt.xlabel('GDP per capita')
df['GDP per capita'].hist()

fig3, ax3 = plt.subplots()

encoded1 = fig_to_base64(fig1)
plot_img1 = '<img src="data:image/png;base64, {}">'.format(encoded1.decode('utf-8'))

encoded2 = fig_to_base64(fig2)
plot_img2 = '<img src="data:image/png;base64, {}">'.format(encoded2.decode('utf-8'))

encoded3 = fig_to_base64(fig3)
plot_img3 = '<img src="data:image/png;base64, {}">'.format(encoded3.decode('utf-8'))

This is the function for the table in my site.

Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
Dat Guy
  • 51
  • 2
  • 6

1 Answers1

0

The method you are looking for is sort_values.

In [2]: data = ({'name': ['Joe', 'Bob', 'Alice', 'Susan'], 'salary': [50000, 65000, 46000, 96000]})

In [3]: employees = pd.DataFrame(data)

In [4]: employees
Out[4]:
    name  salary
0    Joe   50000
1    Bob   65000
2  Alice   46000
3  Susan   96000

In [5]: employees.sort_values(by='salary', ascending=False)
Out[5]:
    name  salary
3  Susan   96000
1    Bob   65000
0    Joe   50000
2  Alice   46000
  • Yeah but I am looking to make it dynamic. – Dat Guy Apr 26 '19 at 14:19
  • Implementation will be based on your specific framework and how it handles callbacks/asynchronous updates. If you haven't chosen one yet you will want to look into something like [Plotly](https://plot.ly/d3-js-for-python-and-pandas-charts/). – CRUDfunction Apr 26 '19 at 14:54
  • I have flask and wtforms. – Dat Guy Apr 26 '19 at 15:26
  • If you want to update the table/graph without reloading the whole page you will [want to use AJAX](http://flask.pocoo.org/docs/0.12/patterns/wtforms/), which can get pretty complicated. Otherwise it should be as simple as passing the user entered data as variables to [a query string](https://stackoverflow.com/questions/24892035/python-flask-how-to-get-parameters-from-a-url), parsing them, and passing them to sort_values() on page reload. – CRUDfunction Apr 26 '19 at 15:59
  • can u give me an example? as I didn't really understand what is a query string. If you can, an example of the code for the sort_values would be helpful too. – Dat Guy Apr 26 '19 at 16:17