RRMC
RRMC

Reputation: 11

Changing the dtype of a row in a dataframe

A new dataframe is returned when the .describe() method is called on a dataframe. In my project, the values in the "counts" row display as floats, with many zeros, which is ugly. A count should be an int, since it is literally a count of the number of values.

I'd like the counts row to display as ints, but I can't find a way to change the dtype of a specific row.

Any suggestions would be greatly appreciated! Thanks.

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(5,3))
df_stats = df.describe()

enter image description here

Upvotes: 1

Views: 1596

Answers (2)

Vini
Vini

Reputation: 935

That simple:

df_stats.iloc[0].astype(int)

Or:

df_stats.loc['count'].astype(int)

Upvotes: 1

piRSquared
piRSquared

Reputation: 294198

Use astype(object)

df_stats.astype(object)

              0          1         2
count         5          5         5
mean   0.551652   0.577811  0.494294
std    0.229048   0.312622   0.28331
min    0.185523  0.0350725  0.136097
25%    0.542329   0.651676  0.275079
50%    0.544838   0.652151  0.538186
75%    0.706685   0.713614   0.74606
max    0.778883   0.836541   0.77605

Upvotes: 2

Related Questions