gpt4 book ai didi

python-3.x - Pandas 读取一个文本文件并根据第一个字符将名称分成几列

转载 作者:行者123 更新时间:2023-12-04 01:53:56 24 4
gpt4 key购买 nike

您好,我期待着看看我们是否可以读取一个文本文件,并根据 pandas 的第一个字符将它们放入单独的列中。

下面是文本文件

$ cat file.txt
AAAAAA
AAAAAA
AAAAAA
AAAAAA
AAAAAA
BBBBBB
BBBBBB
BBBBBB
BBBBBB
BBBBBB
CCCCCC
CCCCCC
CCCCCC
CCCCCC
CCCCCC
DDDDDD
DDDDDD
DDDDDD
DDDDDD
DDDDDD
EEEEEE
EEEEEE
EEEEEE
EEEEEE
EEEEEE
FFFFFF
FFFFFF
FFFFFF
FFFFFF
FFFFFF

期望:

COL_1   COL_2   COL_3   COL_4   COL_5   COL_6
AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE FFFFFF
AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE FFFFFF
AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE FFFFFF
AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE FFFFFF
AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE FFFFFF

最佳答案

可能不是最好的方法:

# notice the header=None option
df = pd.read_csv('file.txt', header=None)

# extract the first character of the string
df['start'] = df[0].str[0]

# group by the first character of the string
# cumcount gives you the order/rank of the row within its group
df['idx'] = df.groupby('start').cumcount()

# pivot - search StackOverflow for 47152691
df.pivot(index='idx', columns='start', values=0)

输出:

start       A       B       C       D       E       F
idx
0 AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE FFFFFF
1 AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE FFFFFF
2 AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE FFFFFF
3 AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE FFFFFF
4 AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE FFFFFF

关于python-3.x - Pandas 读取一个文本文件并根据第一个字符将名称分成几列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56905959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com