-1

我有一个仅包含一列的文本文件,我想为该列添加文本,但不想替换原始列。只有在将文本文件中的第一列手动分配为“0”而不是原始列名“a、b、c、d、e、f、g、h、i、j、 k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"。我的代码如下:

import pandas as pd

df=pd.read_csv("oss1.txt",sep='\t',engine='python')

df['new']=df['0'].copy()

def text_to_column(df, col):
    df_detailed = df[col].str.rstrip(':| ').str.split(':| ', expand=True).astype(str)
    #replace columns names if necessary
    df_detailed.columns = df_detailed.columns.to_series().replace(":| ", ":| ")
    #remove column and join new df
    df_detailed = df.drop(col, axis=1).join(df_detailed)

    return df_detailed

df = text_to_column(df,'new')

df.to_excel("output_oss1.xlsx", index = False)

i/p

a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
apple, boy,cat,dog,egg,fish,girl,hen,ink,jug,kite,lion,man,net,owl,pen,queen,rat,seat,ten,umbrella,va,watch,xmas,yak,zebra
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26

o/p

a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p   q   r   s   t   u   v   w   x   y   z
apple, boy,cat,dog,egg,fish,girl,hen,ink,jug,kite,lion,man,net,owl,pen,queen,rat,seat,ten,umbrella,va,watch,xmas,yak,zebra  apple   boy cat dog egg fish    girl    hen ink jug kite    lion    man net owl pen queen   rat seat    ten umbrella    van watch   xmas    yak zebra
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26    1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26
  • 项目清单
4

1 回答 1

0

将一列中的文本拆分为多列的一种方法 -

df.str.split(",", expand=True)

参考文档

于 2019-08-08T13:38:15.053 回答