python - 字符串在表中每隔 n 个空格分割一行

标签 python pandas dataframe

我有这个数据集,我试图在其中拆分 OrtoB 列,以便在多对多交互中将我的数据从 A 组织到 B。

示例数据集

   new_name  Score            OrtoA                                      OrtoB
0         1   3064   g2797.t1 1.000                              YHR165C 1.000
1         2   2820   g2375.t1 1.000                              YJL130C 1.000
2         3   2711   g1023.t1 1.000                              YLR106C 1.000
3         4   2710  g15922.t1 1.000                              YNR016C 1.000
4         5   2568   g3549.t1 1.000                              YDL171C 1.000
5         6   2494  g10464.t1 1.000  YOR153W 1.000 YDR406W 0.585 YOR328W 0.454
6         7   2402  g15604.t1 1.000                YGR032W 1.000 YLR342W 0.679

到目前为止,我已经能够通过在 python 中使用下面的代码来分割字符串,并按照之前回答的帖子 pandas: How do I split text in a column into multiple rows? 中的示例进行操作。 .

z = pd.read_table("table.augustus",header=0)
col_name =z.columns[0]
z = z.rename(columns = {col_name:'new_name'}
z['OrtoB'].str.split(" ").apply(Series,1).stack()

但是,只有当我尝试分割的空间只有一个时,它才有效。我正在寻找的是在每两个空格上进行分割以获得如下结果的帮助。

期望的结果

        OrtoA       OrtoB   
g2797.t1    1   YHR165C 1
g2375.t1    1   YJL130C 1
g1023.t1    1   YLR106C 1
g15922.t1   1   YNR016C 1
g3549.t1    1   YDL171C 1
g10464.t1   1   YOR153W 1
g10464.t1   1   YDR406W 0.585
g10464.t1   1   YOR328W 0.454
g15604.t1   1   YGR032W 1
g15604.t1   1   YLR342W 0.679

最佳答案

您可以使用:

#column into lists
orto = z['OrtoB'].str.split()
#remove all empty lists
orto = orto[orto.astype(bool)]
#get lengths of lists, but floor divide by 2 because pairs
lens = orto.str.len() // 2
#explode nested lists to array
orto2 = np.concatenate(orto.values)
#repeat index to explode
idx = z.index.repeat(lens)
#create DataFrame and join both column together
s = pd.DataFrame(orto2.reshape(-1,2), index=idx).apply(' '.join, axis=1).rename('OrtoB')
#remove original column and join s
z = z.drop('OrtoB', axis=1).join(s).reset_index(drop=True)
print (z)
   new_name  Score            OrtoA          OrtoB
0         1   3064   g2797.t1 1.000  YHR165C 1.000
1         2   2820   g2375.t1 1.000  YJL130C 1.000
2         3   2711   g1023.t1 1.000  YLR106C 1.000
3         4   2710  g15922.t1 1.000  YNR016C 1.000
4         5   2568   g3549.t1 1.000  YDL171C 1.000
5         6   2494  g10464.t1 1.000  YOR153W 1.000
6         6   2494  g10464.t1 1.000  YDR406W 0.585
7         6   2494  g10464.t1 1.000  YOR328W 0.454
8         7   2402  g15604.t1 1.000  YGR032W 1.000
9         7   2402  g15604.t1 1.000  YLR342W 0.679

关于python - 字符串在表中每隔 n 个空格分割一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43688404/

相关文章:

python - Pandas 在每第 n 行后插入一个新行

python - 从字符串转换到列表中的倒数第二个条目后舍入 float

javascript - ajax 将参数传递给 python 脚本

Python Pandas - 将绝对周期转换为相对周期

python - 如何从 .log 文件在 .csv 文件中创建行和列

python - 按 timedelta 修剪 TimeSeries

python - Discord.py - 'VoiceState' 对象没有属性 'voice_channel'

python - Windows [Python] 中的 signal.alarm 替换

python - pandas 将字符串的类别转换为数字作为一个对象,但得到一组数字

python - 向 Pandas 数据框添加更多列