2

I have multiple column that I need to slice with same condition I can see some solution here but they do not work with multiple column

df['x', 'y']=df['x', 'y'].str.slice(0,19)

Error

AttributeError: 'DataFrame' object has no attribute 'str'

2 Answers2

0

You need to use the apply function to a apply it on multiple columns:

df[['x','y']]=df[['x','y'].apply(lambda x:x.str.slice(0,19))

It should give the correct output.

Walid
  • 718
  • 5
  • 13
0

You may use applymap() method

import pandas as pd

df_exp = pd.DataFrame([('ABCDEF', 'GHIJKL')], columns=["x", "y"])

print(df_exp.head(10))
#         x       y
# 0  ABCDEF  GHIJKL

your_slice_func = lambda x: x[0:3]
df_result = df_exp.applymap(your_slice_func)

print(df_result.head(10))
#      x    y
# 0  ABC  GHI

sagmansercan
  • 101
  • 4