Pandas dataframe: truncate string fields

后端 未结 3 2082
傲寒
傲寒 2021-01-03 23:07

I have a dataframe and would like to truncate each field to up to 20 characters. I\'ve been naively trying the following:

df = df.astype(str).apply(lambda x:         


        
3条回答
  •  耶瑟儿~
    2021-01-03 23:53

    I think need str for indexing with str:

    df = df.astype(str).apply(lambda x: x.str[:20])
    

    Sample:

    df = pd.DataFrame({'A':[1,2,3],
                       'B':[4,5,6],
                       'C':[7,8,9],
                       'D':[1,3,5],
                       'E':[5,3,6],
                       'F':[7,4,3]}) * 1000
    
    print (df)
          A     B     C     D     E     F
    0  1000  4000  7000  1000  5000  7000
    1  2000  5000  8000  3000  3000  4000
    2  3000  6000  9000  5000  6000  3000
    
    df = df.astype(str).apply(lambda x: x.str[:2])
    print (df)
        A   B   C   D   E   F
    0  10  40  70  10  50  70
    1  20  50  80  30  30  40
    2  30  60  90  50  60  30
    

    Another solution with applymap:

    df = df.astype(str).applymap(lambda x: x[:2])
    print (df)
        A   B   C   D   E   F
    0  10  40  70  10  50  70
    1  20  50  80  30  30  40
    2  30  60  90  50  60  30
    

    Problem of your solution is if x[:20] select only first 20 rows in each column.

    You can test it by custom function:

    def f(x):
        print (x)
        print (x[:2])
    
    df = df.astype(str).apply(f)
    print (df)
    

提交回复
热议问题