【发布时间】:2022-12-28 01:08:42
【问题描述】:
I have 2 columns:
| A | B |
|---|---|
| 1 | ABCSD |
| 2 | SSNFs |
| 3 CVY KIP | |
| 4 MSSSQ | |
| 5 | ABCSD |
| 6 MMS LLS | |
| 7 | QQLL |
This is an example actual files contains these type of cases in 1000+ rows. I want to separate all the alphabets from column A and get them as output in column B: Expected Output:
| A | B |
|---|---|
| 1 | ABCSD |
| 2 | SSNFs |
| 3 | CVY KIP |
| 4 | MSSSQ |
| 5 | ABCSD |
| 6 | MMS LLS |
| 7 | QQLL |
So Far I have tried this which works but looking for a better way:
df['B2'] = df['A'].str.split(' ').str[1:]
def try_join(l):
try:
return ' '.join(map(str, l))
except TypeError:
return np.nan
df['B2'] = [try_join(l) for l in df['B2']]
df = df.replace('', np.nan)
append=df['B2']
df['B']=df['B'].combine_first(append)
df['A']=[str(x).split(' ')[0] for x in df['A']]
df.drop(['B2'],axis=1,inplace=True)
df
【问题讨论】:
-
What have you tried so far?
-
Edited , you can see my approach now