Formatting a custom line inside a series in Pandas

I am looking for an individual version of a method Series.str.zfill(width). This method adds zeros to the string, so the string has widthcharacters. I am looking for something that does this, but with any characters (or a sequence of characters), and not just 0. For example, adding '-' as many times as necessary on the left so that the string has widthcharacters.

+4
source share
2 answers

I think you are looking for Series.str.rjustone that accepts widthand fillcharas arguments:

Filling the left side of the lines in the Series / Index with an additional character.

+3

, left_fill , pandas.Series.apply .

import pandas as pd

s = pd.Series(['foo', 'bar', 'baz', 'apple'])

def left_fill(string, char, length):
    while len(string) < length:
        string = char + string
    return string

s.apply(left_fill, args = ('-', 5))

, , rjust pandas ! - , .

+1

Source: https://habr.com/ru/post/1667364/


All Articles