如何将数组加载为数据框中的新列? [英] How to load an array as a new column in a dataframe?

查看:18
本文介绍了如何将数组加载为数据框中的新列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生成一个数组作为时间序列计算,现在我想将生成的数组作为新列包含在我的数据框中,该列作为 xlsx 文件加载.稍后我将在函数中将其用作数据提供者.

I generate an array as a time series calculation, and now I would like to include the generated array as a new column in my dataframe, which is loaded as a xlsx file. I will use it later as data provider in a function.

我该怎么做??

import th

'Let´s suppose that time, PointA_ini, PointB_ini and Ge already are loaded'

#Here, I call a function which calculates an array over time. As a example, pressure over time
example = th.overtime(time, PointA_ini, PointB_ini, Ge)
Output = example.results()
print(Output)

# An array with a value each step is shown.
# Now, I want to include the array in my generated dataframe:
""" - - - - dframe- - - - - -"""
xls = pd.ExcelFile('Int1.xlsx')
df = xls.parse('Sheet1')
#HOW CAN I INCLUDE IN MY DF MY LIST AS A COLUMN???

推荐答案

首先,除非你在别处定义了 Salidas,否则你会遇到问题.我猜你的意思是,print(Output)?

First of all, unless you defined Salidas elsewhere, you are going to have a problem. I assume you meant, print(Output)?

至于你的问题,如果 Output 已经是一个列表,你所要做的就是将它分配给一个新列:

As for your question, if Output is already a list, all you have to do is assign it to a new column:

df['output'] = Output

如果它是某种其他类型的可迭代对象(例如 Numpy 数组),则首先需要将其包装在 Series 中:

If it is some other kind of iterable (such as a Numpy array), you will first need to wrap it in a Series:

df['output'] = pd.Series(Output)

这篇关于如何将数组加载为数据框中的新列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆