I have this code:
df = pd.DataFrame([('bird', 'Falconiformes', 389.0),
('bird', 'Psittaciformes', 24.0),
('mammal', 'Carnivora', 80.2),
('mammal', 'Primates', np.nan),
('mammal', 'Carnivora', 58)],
index=['falcon', 'parrot', 'lion', 'monkey', 'leopard'],
columns=('class', 'order', 'max_speed'))
And I want to regroup the sames values so:
gp = df.groupby(['class', 'order','max_speed'])["class"].size().frame()
I got this:
class
class order max_speed
bird Falconiformes 389.0 1
Psittaciformes 24.0 1
mammal Carnivora 58.0 1
80.2 1
I put it on a html file, but what is displayed is just the last column while I want the first three. Any ideas?
Thanks !
try using the flag index=True
in your case, since other 3 columns are in the index when you applied groupby
df = pd.DataFrame([('bird', 'Falconiformes', 389.0),
('bird', 'Psittaciformes', 24.0),
('mammal', 'Carnivora', 80.2),
('mammal', 'Primates', np.nan),
('mammal', 'Carnivora', 58)],
index=['falcon', 'parrot', 'lion', 'monkey', 'leopard'],
columns=('class', 'order', 'max_speed'))
> df
class order max_speed
falcon bird Falconiformes 389.0
parrot bird Psittaciformes 24.0
lion mammal Carnivora 80.2
monkey mammal Primates NaN
leopard mammal Carnivora 58.0
gp = df.groupby(['class', 'order','max_speed'])["class"].size().to_frame()
class
class order max_speed
bird Falconiformes 389.0 1
Psittaciformes 24.0 1
mammal Carnivora 58.0 1
80.2 1
# All columns are printed in html
gp.to_html('D:/sample.html', index=True)
# Only last column, which is not in index gets printed in HTML.
gp.to_html('D:/sample.html', index=False)
Edit
I have this error: SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
The above error is a decoding
error since you have imported excel from a file with a different encoding. To resolve this, pass the proper encoding format using the encoding
flag.
In your case,
gp.to_html('C:\Users\gregd\PycharmProjects\sample.html', index=True, encoding='utf-8')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.