python - 将数据帧结果连接到另一个数据帧
问题描述
所以我想尝试将数据帧结果连接到另一个。基本上,我有 for 循环遍历要放入函数的列表的每个元素以及要连接到另一个数据帧的结果函数。我尝试使用下面的代码,但变量“final”没有连接数据帧,只是得到了我循环的最后一个结果。任何人都可以帮助我吗?谢谢你
for i in range(len(gene_result)):
result = pd.DataFrame()
test = dummy_fitness(gene_result[i]) # function that resulted into dataframe
coords = 'gen '+ str(i) +' coords'
fitness_points = 'gen ' + str(i) + ' points'
result[coords] = test.pudo_coords # getting the particular column i want
result[fitness_points] = test.fit # getting the particular column i want
final = pd.concat([result])
“结果”变量如下所示,每个循环都会有所不同。我想将变量“结果”连接到变量“最终”
gen 0 coords gen 0 points
0 -6.18360560071739,106.833802730435 8.128732
1 -6.19583084820588,106.850715008823 6.396317
2 -6.17002786742308,106.840075203846 6.050418
3 -6.1956966496,106.822705886667 5.976020
4 -6.18757562077778,106.845673922222 5.703797
5 -6.18317963676,106.81405708 5.622984
6 -6.18141226474074,106.822889814815 5.564183
解决方案
我认为您需要DataFrame
通过附加然后concat
在外部循环创建 s 列表:
dfs = []
for i in range(len(gene_result)):
result = pd.DataFrame()
test = dummy_fitness(gene_result[i]) # function that resulted into dataframe
coords = 'gen '+ str(i) +' coords'
fitness_points = 'gen ' + str(i) + ' points'
result[coords] = test.pudo_coords # getting the particular column i want
result[fitness_points] = test.fit # getting the particular column i want
dfs.append(result)
final = pd.concat(dfs, axis=1)
推荐阅读
- excel - Excel查找和替换函数正确公式
- couchdb - How to set attachment size limit for an individual database
- c# - .net core console authenticate to AWS Cognito onpremise AD FS SAML
- mysql - AS compatibility: “.. from T as a..” works in MySQL and SQL Server, not Oracle?
- c++ - How to pick points inside a circle in OpenGL?
- dialogflow-es - How to migrate dialogflow from V2beta1 to V2?
- ios - 为什么在未设置 AudioSession 的情况下播放 AVPlayer/AVQueuePlayer 时背景音乐停止?
- c++ - IDP.Generic when i try to run a dll injector in visual studio. Is this safe to run? (can i even get a virus from VS?)
- javascript - 如何在 React 和 Ant Design 中的图像之间切换可见性设置?
- javascript - 通过vuejs在新窗口中打开pdf