python - 对于每次迭代,只将数据添加到 pandas 中空数据框的几列
问题描述
我有一个代码,其中有一个循环,每次迭代的输出都创建一个df1
具有不同列名的数据框,比如在迭代 1 中,df1
创建的数据框给我列 A、B、C,在迭代 2 中列是 A ,X,D。每个数据帧只有一条记录作为输出。
总共可以有 26 个不同的列和一个 iteration_seq 列。df2
我创建了一个包含所有可能列的 pandas 空数据框。
df2 = pd.DataFrame(columns = ['iteration_seq','A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z'])
lis = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T','U', 'V', 'W', 'X', 'Y', 'Z']
import random
random.shuffle(lis)
import numpy as np
for i in range(len(lis)-1):
pax = lis[i] # we have problem from here, say I want to take any aribitrary variable and put random value in that, and then update the df2, and later append the df2 in the df1
df2 = pd.DataFrame(columns = ['iteration_seq'])
df2[pax] = np.random.rand(1,1000)) # and how to take a variable value as column value, in R we had the option of "with"
df2['iteration_seq'] = i+1
如何将循环的每次迭代的值作为空数据帧 df2 中的单独行仅用于 df1 中循环输出中的列,以及指定迭代次数的附加列。
解决方案
推荐阅读
- c - function() 中未指定的参数会发生什么?
- c# - 尝试连接到 Ms Access Db,但我没有管理员权限或 ACE.OLEDB.16.0
- node.js - 尝试搜索具有关联多对多续集的实体
- docker - Docker 镜像构建以悬空镜像结束
- python - 路由到从下拉列表中单击的页面
- sockets - 我正在尝试在不使用套接字的情况下使用 GET,但我不断收到 400 Bad Request
- kotlin - 从 RecyclerView 适配器内部访问 AlarmManager
- reactjs - 如何在 ReactJs 应用程序中读取 Spring Boot application.properties?
- python - tkinter 中的“鼠标悬停”事件
- mysql - 如何在MySQL中检查X是否是Y的孩子,反之亦然?