尝试使用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)

这是我的代码,下面的图像是结果。我想存储所有特征的列值,但只存储最后一列。

您可以简单地使用df.to_dict(orient="list")将Pandas数据框转换为字典。

最新更新