简体   繁体   English

从两列修剪和连接字符串:python

[英]Trim and concatenation of string from two column: python

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.我的df为 2 列。

|       | 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)这里00来自(col, row) = (1,2)A来自(col, row) = (2,1)

another example is另一个例子是

11B

I want to get all the strings as possible from the df .我想从df中获取所有字符串。 Thanks in advance!提前致谢!

If I'm understanding correctly, you can index the str and concatenate the shift of 1 row.如果我理解正确,您可以索引 str 并连接 1 行的移位。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM