gpt4 book ai didi

python - 如何解析 csv 数据,将某些行显示为列

转载 作者:行者123 更新时间:2023-12-01 07:29:07 24 4
gpt4 key购买 nike

我想将行值显示为特定列中的值的列

原始数据

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com