我是python中的新手,我想转换一个CSV格式的文件。我的文件结构如下:
Country;Class;Number
UK;1;50
Germany;2;30
France;3;50
France;1;20
我需要将类变量转换为列,即
class1 class2
country Number Number
是否可以在Shell或Python中执行此操作?
我知道可以在Python中使用zip()
完全转置矩阵,但我只想转置class
- 列。是可以在Python或shellcript中做到这一点吗?
答案 0 :(得分:1)
您想要转动,而不是转置数据:
import pandas
from io import StringIO # python 3
# from StringIO import StringIO # python 2
datafile = StringIO("""\
Country;Class;Number
UK;1;50
Germany;2;30
France;3;50
France;1;20
""")
df = pandas.read_csv(datafile, sep=';')
print(df.pivot(index='Country', columns='Class', values='Number'))
Class 1 2 3
Country
France 20 NaN 50
Germany NaN 30 NaN
UK 50 NaN NaN
答案 1 :(得分:0)
import csv
with open('test.csv', 'r') as f:
reader = csv.DictReader(f, delimiter=';')
# make a dict of empty lists, to contain our values
transposed = {k:[] for k in reader.fieldnames} # could use defaultdict here
for row in reader:
for k, v in row.items():
transposed[k].append(v)
print(transposed)
这会给你一个字典,如下所示:
{
'Country': ['UK', 'Germany', 'France', 'France'],
'Class': ['1', '2', '3', '1'],
'Number': ['50', '30', '50', '20']
}
加分:您还可以使用collections.defaultdict
而非使用词典理解来设置transposed
:
from collections import defaultdict
transposed = defaultdict(list)