Python 3.x 将数组和变量写入数据帧
我有一个格式为Python 3.x 将数组和变量写入数据帧,python-3.x,pandas,string,numpy,Python 3.x,Pandas,String,Numpy,我有一个格式为[27.214 27.566]的数组-可以有几个数字。此外,我还有一个Datetime变量 now=datetime.now() datetime=now.strftime('%Y-%m-%d %H:%M:%S') time.sleep(0.5) agilent.write("MEAS:TEMP? (@101:102)") values = np.fromstring(agilent.read(), dtype=float, sep=',
[27.214 27.566]
的数组-可以有几个数字。此外,我还有一个Datetime变量
now=datetime.now()
datetime=now.strftime('%Y-%m-%d %H:%M:%S')
time.sleep(0.5)
agilent.write("MEAS:TEMP? (@101:102)")
values = np.fromstring(agilent.read(), dtype=float, sep=',')
阵列的输出为[27.214 27.566]
现在,我想将其写入具有以下结构的数据帧中:
Datetime,FirstValueArray,SecondValueArray,….
如何做到这一点?在数据帧中,每一分钟会添加一个新数组。我假设您希望将一行附加到现有数据帧
df
,并使用适当的列:value1,value2,…,lastvalue,datetime
我们可以轻松地将阵列转换为系列:
s=pd.系列(阵列)
接下来要做的是将datetime值附加到序列中:
s.append(datetime,ignore\u index=True)
cf
现在,您有了一个长度与df.columns
匹配的序列。您希望将该系列转换为数据帧,以便能够使用pd.concat
:
df_to_append=s.to_frame().T
我们需要获得原始数据帧的转置,因为Series.to_frame()
返回一个数据帧,其中序列作为一个列,我们需要一个索引,但需要多个列
但是,在连接之前,需要确保这两个dataframes列名称匹配,否则将创建其他列:
df\u to\u append.columns=df.columns
现在我们可以连接两个数据帧:
pd.concat([df,df\u to\u append],ignore\u index=True)
cf
有关更多详细信息,请参见请创建一个可复制的示例。请发布一个带有预期输出的示例输入。我已经编辑了该帖子