StayFoolish
StayFoolish

Reputation: 521

pandas DataFrame/Series value formatting issue

Let me give an example:

df = pd.DataFrame(np.arange(6).reshape(3, 2), columns=list('ab'))
print(df)

   a  b
0  0  1
1  2  3
2  4  5

say, I want to select a row with column 'a' == 0, and I know that in my dataframe that there is only one row satisfies this condition.

df1 = df.loc[df['a'] == 0]
print(df1)
   a  b
0  0  1
type(df1)
pandas.core.frame.DataFrame

df2 = df.loc[0]
print(df2)
a    0
b    1
Name: 0, dtype: int32
type(df2)
pandas.core.series.Series

As you can see, df1 is a DataFrame instance, but df2 is a Series, although df1 only has one row.

Now the problem occurs when I try to format the value of df1:

print('{:.2f}'.format(df1['a']))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-92-62c2a3e8dbc0> in <module>()
----> 1 print('{:.2f}'.format(df1['a']))

TypeError: unsupported format string passed to Series.__format__

but it's ok to print the value of df2.

print('{:.2f}'.format(df2['a']))
0.00

I understand that it's because df1 is a DataFrame, df1['a'] would be a Series, but the argument passed to format() function expects something other than a Series object. So I tried to walk around this awkwardly:

print('{:.2f}'.format(df1['a'].values[0]))
0.00

Is there anyway that's more efficient and pythnoic here?

Upvotes: 2

Views: 5469

Answers (1)

Darius
Darius

Reputation: 12072

If you want to change the data type to str, you can use:

df = df[df['a'] == 0].astype(str)

Result print(df):

   a  b
0  0  1

Datatypes print(df.dtypes):

a    object
b    object
dtype: object

If you want to apply a string format, you can use:

df = df[df['a'] == 0].applymap('{:,.2f}'.format)

Result print(df):

      a     b
0  0.00  1.00

Datatypes print(df.dtypes):

a    object
b    object
dtype: object

The next solution doesn't change the data.

pattern = '{:,.2f}'.format
print df.to_string(formatters={'a': pattern, 'b': pattern})

Output:

     a    b
0 0.00 1.00
1 2.00 3.00
2 4.00 5.00

Upvotes: 4

Related Questions