It seems to me that str for indexing with a string :
df = df.astype(str).apply(lambda x: x.str[:20])
Example:
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) ABCDEF 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) ABCDEF 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) ABCDEF 0 10 40 70 10 50 70 1 20 50 80 30 30 40 2 30 60 90 50 60 30
The problem with your solution is that if x[:20] select only the first 20 rows in each column.
You can test it by user function:
def f(x): print (x) print (x[:2]) df = df.astype(str).apply(f) print (df)
source share