Trim and concatenation of string from two column: python

Question:

I have tried a ton of ways but none of worked! I would greatly appreciate if someone give any solutions or suggestions.

I have a df of 2 column.

|       | col 1   | col 2 |
|-------|---------|-------|
| row 1 | ε       | A     |   
| row 2 | aa/#/00 | ε     |   
| row 3 | ε       | B     |   
| row 4 | bb/#/11 | ε     |   
|  ...  |   ...   |  ...  |  
|  ...  |   ...   |  ...  | 

Now I want to generate a string. for example,

00A  

here 00 is from (col, row) = (1,2)
and A is from (col, row) = (2,1)

another example is

11B

I want to get all the strings as possible from the df.
Thanks in advance!

Asked By: XO56

||

Answers:

If I’m understanding correctly, you can index the str and concatenate the shift of 1 row.

import pandas as pd


data = {
    "col1": ["E", "aa/#/00", "E", "bb/#/11"],
    "col2": ["A", "E", "B", "E"]
}

df = pd.DataFrame(data)
df["generated"] = df["col1"].str[-2:] + df["col2"].shift(1)
print(df)

      col1 col2 generated
0        E    A       NaN
1  aa/#/00    E       00A
2        E    B        EE
3  bb/#/11    E       11B
Answered By: snake_charmer_775