[Python] how to do a left,right and mid of a string in a pandas dataframe

Discussão em 'Python' iniciado por Stack, Julho 25, 2025.

  1. Stack

    Stack Membro Participativo

    in a pandas dataframe how can I apply a sort of excel left('state',2) to only take the first two letters. Ideally I want to learn how to use left,right and mid in a dataframe too. So need an equivalent and not a "trick" for this specific example.

    data = {'state': ['Auckland', 'Otago', 'Wellington', 'Dunedin', 'Hamilton'],
    'year': [2000, 2001, 2002, 2001, 2002],
    'pop': [1.5, 1.7, 3.6, 2.4, 2.9]}
    df = pd.DataFrame(data)

    print df

    pop state year
    0 1.5 Auckland 2000
    1 1.7 Otago 2001
    2 3.6 Wellington 2002
    3 2.4 Dunedin 2001
    4 2.9 Hamilton 2002


    I want to get this:

    pop state year StateInitial
    0 1.5 Auckland 2000 Au
    1 1.7 Otago 2001 Ot
    2 3.6 Wellington 2002 We
    3 2.4 Dunedin 2001 Du
    4 2.9 Hamilton 2002 Ha

    Continue reading...

Compartilhe esta Página