将数据列值重新整形为标题

时间:2018-04-10 00:47:48

标签: python pandas sqlite

我有一个不断增长的数据集(让我们称之为ADC),其中每一行看起来像这样:

AK1,2018-03-27,22:42:21,AKDT,Running Feedback calls in a loop,Iteration 1,AIN0(SE),0.091 ,AIN1(SE),-0.007 ,AIN2(SE),-0.008 ,AIN3(SE),3.659 ,Temperature,283.824,Iteration 2,AIN0(SE),0.096 ,AIN1(SE),-0.007 ,AIN2(SE),-0.003 ,AIN3(SE),3.649 ,Temperature,283.824,Iteration 3 ...

通过迭代5. Picture in case it doesn't make sense inline

因此,您将有5个完整的“AIN0”,“AIN1”列,依此类推,相邻的5个值的值。我想为每个迭代创建一个新行,它保留原始行中前5列的值,并删除AIN *,Temperature列,使它们成为当前每个值后面的值的列名,所以它看起来like this。我尝试过使用reshape,stack,wide_to_long的各种方法,但它从来都不行。

ADC.melt(id_vars=ADC.columns[0:4]) gives me something like this

pd.wide_to_long(ADC,stubnames =“Iteration”,i = [ADC.iloc [:,1:4]],j =“数字”)

抛出此错误:ValueError:stubname不能与列名相同

我哪里错了?

编辑:感谢评论@DJK,这里是数据框的可复制,截断版本:

AK1,2018-03-27,22:42:21,AKDT,Running Feedback calls in a loop,Iteration 1,AIN0(SE),0.091,AIN1(SE),-0.007,AIN2(SE),-0.008,AIN3(SE),3.659,Temperature,283.824,Iteration 2,AIN0(SE),0.096,AIN1(SE),-0.007,AIN2(SE),-0.003,AIN3(SE),3.649,Temperature,283.824
AK1,2018-03-27,22:47:05,AKDT,Running Feedback calls in a loop,Iteration 1,AIN0(SE),0.101,AIN1(SE),-0.007,AIN2(SE),-0.003,AIN3(SE),3.725,Temperature,285.634,Iteration 2,AIN0(SE),0.101,AIN1(SE),-0.007,AIN2(SE),-0.003,AIN3(SE),3.725,Temperature,285.634
AK1,2018-03-28,00:32:58,AKDT,Running Feedback calls in a loop,Iteration 1,AIN0(SE),0.116,AIN1(SE),1.399,AIN2(SE),-0.008,AIN3(SE),3.872,Temperature,290.462,Iteration 2,AIN0(SE),0.121,AIN1(SE),1.399,AIN2(SE),-0.003,AIN3(SE),3.933,Temperature,290.462
AK1,2018-03-28,09:33:19,AKDT,Running Feedback calls in a loop,Iteration 1,AIN0(SE),0.101,AIN1(SE),1.399,AIN2(SE),0.012,AIN3(SE),3.71,Temperature,258.479,Iteration 2,AIN0(SE),0.101,AIN1(SE),1.405,AIN2(SE),0.012,AIN3(SE),3.705,Temperature,258.479

我真的希望它看起来像(在开头添加一些额外的空格可能有助于提高可读性):

Name, Date,Time,TZ,Process,Iteration,AIN0(SE),AIN1(SE),AIN2(SE),AIN3(SE),Temperature
AK1,  2018-03-27, 22:42:21, AKDT,Running Feedback calls in a loop,1,0.091,-0.007,-0.008,3.659,283.824
AK1,  2018-03-27, 22:42:21, AKDT,Running Feedback calls in a loop,2,0.096,-0.007,-0.003,3.649,283.824
AK1,  2018-03-27, 22:47:05, AKDT,Running Feedback calls in a loop,1,0.101,-0.007,-0.003,3.725,285.634,
AK1,  2018-03-27, 22:47:05, AKDT,Running Feedback calls in a loop,2,0.101,-0.007,-0.003,3.725,285.634
AK1,  2018-03-28, 00:32:58, AKDT,Running Feedback calls in a loop,1,0.116,1.399,-0.008,3.872,290.462
AK1,  2018-03-28, 00:32:58, AKDT,Running Feedback calls in a loop,2,0.121,1.399,-0.003,3.933,290.462
AK1,  2018-03-28, 09:33:19, AKDT,Running Feedback calls in a loop,1,0.101,1.399,0.012,3.71,258.479,
AK1,  2018-03-28, 09:33:19, AKDT,Running Feedback calls in a loop,2,0.101,1.405,0.012,3.705,258.479

更新: 我通过使用pd.read_csv(usecols =)将CSV读入多个数据框(每次迭代一个)来找到解决方案,仅选择包含数据的列并手动命名列。然后我使用pd.concat来组合数据帧和手动分配的indecies。

import pandas as pd

adc_names=["name", "date", "time", "tz", "iteration", "AIN0(SE)", "AIN1(SE)", "AIN2(SE)", "AIN3(SE)", "temperature"]

Iteration1 = pd.read_csv('ADC_CLEAN.csv', index_col=False, header=None, usecols=[0,1,2,3,5,7,9,11,13,15], names=adc_names)
Iteration2 = pd.read_csv('ADC_CLEAN.csv', index_col=False, header=None, usecols=[0,1,2,3,16,18,20,22,24,26], names=adc_names)
Iteration3 = pd.read_csv('ADC_CLEAN.csv', index_col=False, header=None, usecols=[0,1,2,3,27,29,31,33,35,37], names=adc_names)
Iteration4 = pd.read_csv('ADC_CLEAN.csv', index_col=False, header=None, usecols=[0,1,2,3,38,40,42,44,46,48], names=adc_names)
Iteration5 = pd.read_csv('ADC_CLEAN.csv', index_col=False, header=None, usecols=[0,1,2,3,49,51,53,55,57,59], names=adc_names)

ADC  = pd.concat([Iteration1, Iteration2, Iteration3, Iteration4, Iteration5], ignore_index=True)
ADC.iteration = ADC.iteration.str.strip('Iteration ')
ADC = ADC.set_index(["name","date","time","tz","iteration"])

0 个答案:

没有答案