Python/Pandas - How to create a "str" column from another numeric, including zero on the left when the value is less than 9

Asked

Viewed 4,942 times

-1

The code I used to create the column is returning with the following error:

The Truth value of a Series is ambiguous. Use a.Empty, a.bool(), a. item(), a.any() or a.all().

I don’t know how to fix this mistake.

import pandas as pd
import numpy as np
raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks'],
            'company': ['1st', '1st', '2nd', '2nd'],
            'deaths': ['kkk', 52, '25', 616],
            'battles': [5, 42, 2, 2],
            'size': ['l', 'll', 'l', 'm']}
df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'deaths', 'battles', 'size'])

def valida_CEP(x):
    if x < 9:
        return '0' + str(x)
    else:
        return str(x)

df['batles_comzero'] = df.apply(valida_CEP(df['battles']),axis=1)

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
  • I am using a notebook jupyter and your example returned the following error: C: Users Antonio Anaconda3 lib site-Packages ipykernel_main_.py:2: Settingwithcopywarning: A value is trying to be set on a copy of a Slice from a Dataframe See the caveats in the Documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#Indexing-view-versus-copy from ipykernel import kernelapp as app.

  • Try using the following expression: df['batles_comzero'] = df.battles.apply(valida_CEP)

1 answer

0

A quick way to do this (TL;DR):

Creating the new column:

df['com_zeros'] = '0'

Applying the condition::

for b in df.itertuples():
    df.com_zeros[b.Index] = '0'+str(b.battles) if b.battles<9 else str(b.battles)

Upshot:

df
     regiment company deaths  battles size com_zeros
0  Nighthawks     1st    kkk        5    l        05
1  Nighthawks     1st     52       42   ll        42
2  Nighthawks     2nd     25        2    l        02
3  Nighthawks     2nd    616        2    m        02

See the example running on repl.it.

Obs.:
The example running on repl.it it seems to lock, but is not the case, the pandas charge in repl.it, is always time consuming.

To suppress warnings on notebook jupyter:

import warnings
warnings.filterwarnings('ignore')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.