Home > Software design >  Trying to store values in dictionary from a data frame using for loop
Trying to store values in dictionary from a data frame using for loop

Time:09-17

#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)

This is my code and the following image is the result. I want to store the column values for all the features but only the last column is getting stored.

CodePudding user response:

You could simply convert a Pandas dataframe to a dictionary using df.to_dict(orient="list").

  • Related