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

问题描述 投票:4回答:3

我想使用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)

任何有关这方面的见解将不胜感激。提前致谢!

python-3.x pandas
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)

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)
© www.soinside.com 2019 - 2024. All rights reserved.