Python-创建数据框,转置并合并以获取表

时间:2019-01-19 01:42:20

标签: python pandas dataframe

我正在学习Python,我有一个问题涉及到每5行创建一个数据框,转置和合并数据框。

我有一个.txt文件,其中包含以下输入。它有成千上万的行,我需要遍历每一行直到文件结尾。

Name,Kamath
Age,23
Sex,Male
Company,ACC
Vehicle,Car
Name,Ram
Age,32
Sex,Male
Company,CCA
Vehicle,Bike
Name,Reena
Age,26
Sex,Female
Company,BARC
Vehicle,Cycle

我需要将此作为输出:

Name,Age,Sex,Company,Vehicle
Kamath,23,Male,ACC,Car
Ram,32,Male,CCA,Bike
Reena,26,Female,BARC,Cycle

1 个答案:

答案 0 :(得分:1)

read_csv用于DataFrame,然后将pivotcumcount用于新索引的计数器:

import pandas as pd

temp=u"""Name,Kamath
Age,23
Sex,Male
Company,ACC
Vehicle,Car
Name,Ram
Age,32
Sex,Male
Company,CCA
Vehicle,Bike
Name,Reena
Age,26
Sex,Female
Company,BARC
Vehicle,Cycle"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.txt'
df = pd.read_csv(pd.compat.StringIO(temp), names=['a','b'])

print (df)
          a       b
0      Name  Kamath
1       Age      23
2       Sex    Male
3   Company     ACC
4   Vehicle     Car
5      Name     Ram
6       Age      32
7       Sex    Male
8   Company     CCA
9   Vehicle    Bike
10     Name   Reena
11      Age      26
12      Sex  Female
13  Company    BARC
14  Vehicle   Cycle

df = pd.pivot(index=df.groupby('a').cumcount(),
              columns=df['a'],
              values=df['b'])
print (df)
a Age Company    Name     Sex Vehicle
0  23     ACC  Kamath    Male     Car
1  32     CCA     Ram    Male    Bike
2  26    BARC   Reena  Female   Cycle