0

I would like to group some strings in the column called 'type' and insert them in a plotly bar, the problem is that from the new table created with groupby I can't extract the x and y to define them in the graph:

tipol1 = df.groupby(['tipology']).nunique()

tipol1

the outpot gives me tipology as index and the grouping based on how many times they repeat

         number data
typology  
  one     2      113
  two     33     33
  three   12     88
  four    44     888
  five    11     66

in the number column (in which I have other values ​​it gives me the correct grouping of the tipology column) Also in the date column it gives me values ​​(I think grouping the dates but not the dates in the correct format) I also found:

tipol=df.groupby(['tipology']).nunique()
tipol2 = tipol[['number']]
tipol2

to take only the number column, but nothing to do, I would need the tipology column (not in index) and the column with the tipology grouping numbers to get the x and y axis to import it into plotly!

One last try I made (making a big mess):

tipol=df.groupby(['tipology'],as_index=False).nunique()
tipol2 = tipol[['number']]


fig = go.Figure(data=[
go.Bar(name='test', x=df['tipology'], y=tipol2)

])

fig.update_layout(barmode='stack')
fig.show()

any suggestions thanks!

UPDATE

I would have too much code to give an example, it would be difficult for me and it would waste your time too. basically I would need a groupby with the addition of a column that would show the grouping value eg:

tipology    Date
home        10/01/18
home        11/01/18
garden      12/01/18
garden      12/01/18
garden      13/01/18
bathroom    13/01/18
bedroom     14/01/18
bedroom     15/01/18
kitchen     16/01/18
kitchen     16/01/18
kitchen     17/01/18

I wish this would happen: by deleting the date column and inserting the value column in the DataFrame that does the count

tipology   value
home         2
garden       3
bathroom     1
bedroom      2
kitchen      3

Then (I'm working with jupyer notebook) leaving the date column and adding the corresponding values ​​to the value column based on their grouping:

  tipology       Date     value
   home        10/01/18     1
   home        11/01/18     1
   garden      12/01/18     2
   garden      12/01/18_____.
   garden      13/01/18     1
   bathroom    13/01/18     1
   bedroom     14/01/18     1
   bedroom     15/01/18     1
   kitchen     16/01/18     2
   kitchen     16/01/18_____.
   kitchen     17/01/18     1

I would need the columns to assign them to the x and y axes to import them to a graph! so none of the columns should be index

scofx
  • 149
  • 12
  • `tipol1 = df.groupby(['tipology']).nunique().reset_index()`? – Quang Hoang Nov 13 '20 at 20:49
  • You can also use this convenient input of the group by `tipol1 = df.groupby('tipology', as_index=False).nunique()` – robertwest Nov 13 '20 at 20:55
  • as I wrote in the comment of the answer, the column tipology after setting index to False returns numeric value (1 in this case) – scofx Nov 13 '20 at 22:01
  • @scofx Please consider sharing a sample of your data like explained [here](https://stackoverflow.com/questions/63163251/pandas-how-to-easily-share-a-sample-dataframe-using-df-to-dict/63163254#63163254), so that those seeking to provide a working suggestion don't have to spend all night recreating your challenge instead of creating an actual answer. – vestland Nov 13 '20 at 22:11
  • 1
    @vestland I made an update to the question to try to explain myself better! – scofx Nov 14 '20 at 22:30

1 Answers1

0

By default the method groupby will return a dataframe where the fields you are grouping on will be in the index of the dataframe. You can adjust this behaviour by setting as_index=False in the group by. Then tipology will still be a column in the dataframe that is returned:

tipol1 = df.groupby('tipology', as_index=False).nunique()
robertwest
  • 904
  • 7
  • 13