我想将行值显示为特定列中的值的列
原始数据
For eg
_time action file
2019-07-24T02:01:02.930-0400 get abc
2019-07-24T00:30:10.927-0400 put abc
2019-07-24T05:01:02.930-0400 get def
2019-07-24T04:30:10.927-0400 put def
等等
我希望输出是
File put get
abc 2019-07-24T00:30:10.927-0400 2019-07-24T02:01:02.930-0400
def 2019-07-24T04:30:10.927-0400 2019-07-24T05:01:02.930-0400
我当时以为可以在for循环中这样做?所以
with open('raw.csv','r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for line in csv_reader:
if line[0] != "":
file = line[0]
if line[1] == "get" and file in {file}
gettime = line[1]
if line[1] == "put" and file in {file}
puttime = line[1]
print file,puttime,gettime
虽然这不起作用
最佳答案
使用pandas.pivot_table
例程:
In [9]: df.pivot_table(index='file', columns='action', values='_time', aggfunc='first')
Out[9]:
action get put
file
abc 2019-07-24T02:01:02.930-0400 2019-07-24T00:30:10.927-0400
def 2019-07-24T05:01:02.930-0400 2019-07-24T04:30:10.927-0400
关于python - 如何解析csv数据,以将某些行显示为列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57298052/