Home > Enterprise >  Python pandas cut first chars and past
Python pandas cut first chars and past

Time:09-16

I have, for example, 2 columns, I need a column no. 2 Copy the first 4 characters and paste up to column no. 1. to match Display column 1 and 2. In each processed header, they have different names, therefore, speaks column numbers

with pd.ExcelFile(file_in, engine="openpyxl") as ex:
    excel = pd.read_excel(ex, sheet, index_col=None)
A B
item1 ABC-1111
item2 BCS-2222

Expected result

A B
ABC-item1 ABC-1111
BCS-item2 BCS-2222

Can I use its number instead of Column Name? (Iloc - dff.iloc[0] = dff.iloc[1].str[:4] IndexError: invalid index to scalar variable.)

CodePudding user response:

IIUC Select first 4 values by str and add to column A:

df.A  =  df.B.str[:4]
  • Related