numpy 如何将一列中的每个字符拆分到各自的列中?

wqnecbli  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(136)

我有一栏的话:

a
0   bear
1    fox
2   wolf
3  mouse

字符串
我需要将其拆分为多个列,以便输出为

0  1  2    3    4
0  b  e  a    r  NaN
1  f  o  x  NaN  NaN
2  w  o  l    f  NaN
3  m  o  u    s    e


请注意,每一行的字符数不同
asarray返回一个值错误:用序列设置数组元素

pn9klfpd

pn9klfpd1#

import pandas as pd
import numpy as np

# Assuming 'a' is a pandas Series
a = pd.Series(['bear', 'fox', 'wolf', 'mouse'])

# Splitting the characters and stacking them as columns
split_chars = a.str.split('', expand=True).drop(0, axis=1)

# Creating 'ao' column by joining the characters
ao = a.str.cat(sep='')

# Printing the results
print(split_chars)
print(ao)

字符串

6psbrbz9

6psbrbz92#

你可以用正则表达式(和lookarounds)split并展开它:

out = df['a'].str.split(r'(?<=.)(?=.)', expand=True)

字符串
splitregex demo
或者,使用extractall获取每个字符,然后使用unstack进行整形:

out = (df['a'].str.extractall('(.)')[0]
       .unstack('match')
       .rename_axis(columns=None)
      )


输出量:

0  1  2    3    4
0  b  e  a    r  NaN
1  f  o  x  NaN  NaN
2  w  o  l    f  NaN
3  m  o  u    s    e

sxpgvts3

sxpgvts33#

另一个可能的解决方案:

pd.concat(list(df['a'].map(lambda x: pd.DataFrame(list(x)))),
          axis=1).T.reset_index(drop=True)

字符串
输出量:

0  1  2    3    4
0  b  e  a    r  NaN
1  f  o  x  NaN  NaN
2  w  o  l    f  NaN
3  m  o  u    s    e

相关问题