python - 尝试使用 for 循环将数据框中的值存储在字典中
问题描述
#Names of columns in the dataframe
featureList = df.columns
print(featureList)
#number of columns in the dataframe
noOfCol = len(df.axes[1])
print(noOfCol)
#number of rows
noOfRow = len(df.axes[0])
print(noOfRow)
featureList = df.columns
print(featureList)
#number of columns in the dataframe
noOfCol = len(df.axes[1])
print(noOfCol)
#number of rows
noOfRow = len(df.axes[0])
print(noOfRow)
#creating a dictionary for storing all the cols separately
#using for loop: way4
dfDic = dict()
colCounter = 0
for featureName in featureList:
dfDic = {featureName:df.iloc[:,[colCounter]]}
colCounter +=1
print(dfDic)
这是我的代码,下图是结果。我想存储所有功能的列值,但只存储最后一列。
解决方案
您可以使用简单地将 Pandas 数据框转换为字典df.to_dict(orient="list")
。
推荐阅读
- google-cloud-platform - Firestore 数据存储模式写入/读取一致性
- c# - System.Data.SqlClient.SqlException:'必须声明标量变量'
- node.js - Socket.IO 连接后节点应用程序退出
- tomcat - 如何在 Jenkins 中创建 2 个单独的 m2 存储库
- cordova - Android Studio 3.5:生成签名包没有得到任何结果
- python-3.x - 如何创建通过循环列表中的所有值返回的 Python 模块或类?
- amazon-web-services - 使用多个模型时推荐什么无服务器模式?
- mysql - SQL select s subtr value 和 real value
- java - 从抛出 OutOfMemoryError Apache POI 的 INPUT-STREAM 加载大型 xlsx 文件
- java - 默认构造函数如何初始化字段