python - 将列表作为列添加到现有数据框
问题描述
我使用数据框(NewList)来计算变量(NPQ),然后我想将此变量(这是一个列表)保存到原始数据框中的一列中。我得到一个错误。
def NPQ_calculation(NewList): #this is the dataframe
Fm_prime = NewList.iloc[0:7, 1] #2nd column in dataframe
NPQ = []
NPQ.append(Fm/Fm_prime - 1) #Fm is the 4th column in dataframe
return NPQ # this is the variable I want to add
# call function for NPQ calculation
NPQ = NPQ_calculation(NewList)
###PROBLEM
NewList['NPQ']= NPQ
这是我收到的错误消息:
ValueError: Length of values does not match length of index
这是我想添加新列(NPQ)的原始数据框[![我使用第 2 列和第 4 列来计算 NPQ,并希望将结果添加到最后一列][2] ][2])
解决方案
制作NPQ
一个DataFrame
然后将其附加到原始框架,例如:
NPQ_data = NPQ_calculation(NewList)
NPQ_df = pd.DataFrame({'NPQ': NPQ_DATA})
NewList = pd.concat([NewList, NPQ_df], axis=1)
注意:发布评论中的答案,以便可以将问题标记为已解决。仍然可能是这篇文章的副本。
推荐阅读
- javascript - Next.js 使用 getStaticPaths 捕获所有未按预期工作的路线
- android - Android Button 以图形布局显示但不在设备上(这不是重复的)
- javascript - 在反应中使用一条线连接两个 div 元素
- python - 填充队列的 Python 线程永远不会执行
- rust - 如何简单地将 &dyn 特征转换为使用?
- python - 通过逻辑应用程序上的功能部署 AZURE 自定义标签表单模型
- javascript - 为什么 d3.arc() 给我函数而不是路径字符串?
- mysql - 在 Linux 服务器上完全通过命令行而不是通过 phpMyAdmin 使用 MySQL 时,如何查找和设置 localhost、用户和密码?
- javascript - 如何在不刷新浏览器或在 React 中使用 useEffect 的情况下检测服务器数据更改?
- django - 更改 HTTP 响应消息 Django