I have a simple pandas dataframe with a column 'mycol' and has 5 rows in it and I'm trying to create 5 new variables for every row value, something like below:
newcol_1=df['mycol'][0] newcol_2=df['mycol'][1] newcol_3=df['mycol'][2] newcol_4=df['mycol'][3] newcol_5=df['mycol'][4]
I don't want to hard code as above and I'm using the below "for loop" but it keeps throwing 'can't assign to operator'. I know that assignment values should be on the right and variable on the left but not sure how do I use for loop to dynamically create these 5 variables.
for i in 0, df.shape[0]-1: #"newcol_"+str(i+1) =df['mycol'][i] # this isn't working newcol_+str(i+1) =df['mycol'][i] # this also isn't working
Appreciate if anyone can help with this...Thanks!
https://stackoverflow.com/questions/67322973/assigning-a-value-from-a-dataframe-to-a-dynamically-created-variable April 30, 2021 at 02:14AM
没有评论:
发表评论