使用Pandas-Python将数据从行移动到创建的列

4
我希望使用pandas在文本文件中移动数据,以便用户能够轻松解析数据。到目前为止,我已经能够导入多个文本文件,并将数据附加到数据框中并添加头部。我想要做的是将数据移动到正确的列,但问题是所有的数据都在同一列中。
这是我的数据:
test2218
math-science-physics
00:00:00:00
00:00:30:00
03-21 04:00:00
28
test2228
math
00:00:00:00
00:00:30:00
03-21 04:00:00
26
test2317
reading-comprehension
00:00:00:00
00:00:30:00
03-21 20:02:00

这是我想要的输出样式:

Test ID     Test Info               Duration_A  Duration_B  Next Use        Participants    
test2218    math-science-physics    00:00:00:00 00:00:30:00 03-21 14:00:00  28
test2228    math                    00:00:00:00 00:00:30:00 03-21 14:00:00  26
test2317    reading-comprehension   00:00:00:00 00:00:30:00 04-11 13:30:00  2

我已经到处查找,但无法找到明确的答案。有人能帮忙吗?

这是我的目前的代码:

import os, glob, pandas as pd
d_frame = []
c_names = ['Test ID', 'Test Info', 'Duration_A', 'Duration_B', 'Next 
           Use', 'Participants']
files_list = glob.glob(os.path.join('C:\\test', '*.txt'))

for file in files_list:
    if os.stat(file).st_size != 0:
    df = pd.read_csv(file, delimiter='\t',header=None, names = c_names)

任何关于此事的见解将不胜感激。提前致谢!

1
你的单列数据框中每一行都是一条记录吗?并且它们始终都是6个信息吗?没有任何信息缺失吗? - Matt W.
看起来确实是系列中的第6个。到目前为止,已经连接了6个文本文件,并且它们在系列中具有相同/类似的数据。 - Hellyeah
3个回答

3

假设您的数据是一个pandas.DataFrame对象,并且这6个信息总是按照特定顺序出现,您可以尝试以下方法:

df = pd.DataFrame({0: ['test2218', 'math-science-physics', '00:00:00:00', '00:00:30:00', '03-21 04:00:00', '28', 'test2228', 'math', '00:00:00:00', '00:00:30:00', '03-21 04:00:00', '26', 'test2317', 'reading-comprehension', '00:00:00:00', '00:00:30:00', '03-21 20:02:00']})

columns = ['Test ID', 'Test Info', 'Duration_A', 'Duration_B', 'Next Use', 'Participants']

df_new = pd.DataFrame(df.groupby(df.index // len(columns))[0].apply(list).values.tolist(), columns=columns)
print(df_new)

    Test ID              Test Info   Duration_A   Duration_B        Next Use    Participants
0  test2218   math-science-physics  00:00:00:00  00:00:30:00  03-21 04:00:00             28 
1  test2228                   math  00:00:00:00  00:00:30:00  03-21 04:00:00             26 
2  test2317  reading-comprehension  00:00:00:00  00:00:30:00  03-21 20:02:00           None

或者,另一种选择是
df_new = pd.DataFrame(df.values.reshape(-1, len(columns)), columns=columns)

2
Chris,感谢你的帮助!我将你的代码应用到了我的代码中,并将列列表更改为我拥有的(c_names)。这个方法完美地解决了我的问题。你太棒了! - Hellyeah
很高兴能帮到你,伙计 :) - Chris Adams

3

下面是使用 numpy.reshape 的简单方法:

import numpy as np
import pandas as pd

pd.DataFrame(np.reshape(df.values, (len(df) // 6, 6)),
             columns=['Test ID', 'Test Info', 'Duration_A', 'Duration_B', 'Next Use', 'Participants'])


    Test ID              Test Info   Duration_A   Duration_B        Next Use    Participants
0  test2218   math-science-physics  00:00:00:00  00:00:30:00  03-21 04:00:00             28 
1  test2228                   math  00:00:00:00  00:00:30:00  03-21 04:00:00             26 
2  test2317  reading-comprehension  00:00:00:00  00:00:30:00  03-21 20:02:00              2

1
import pandas as pd

x= pd.Series(['test2218',
'math-science-physics',
'00:00:00:00',
'00:00:30:00',
'03-21 04:00:00',
'28',
'test2228',
'math',
'00:00:00:00',
'00:00:30:00',
'03-21 04:00:00',
'26',
'test2317',
'reading-comprehension',
'00:00:00:00',
'00:00:30:00',
'03-21 20:02:00',
'55'])

循环查找所需的索引。
indices = []
for i in range(6):
    indices.append(list(range(i, len(x), 6)))

创建一个列列表和空数据框,然后循环遍历索引,将其子集分配给数据框。
columns=['Test ID', 'Test Info', 'Duration_A', 'Duration_B', 'Next Use', 'Participants']
df = pd.DataFrame({})
for col, ixs in zip(columns, indices):
    df[col] = x[ixs].reset_index(drop=True)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接