I have a code that return the max value of each column in the dataframe until now it returns a list of the correct items.
What I want is to assign each item into a variable where this variable is created inside the for loop over the list.
expected result:
where the var is a variable created by the iteration over the list.
if i run the code below it crash and return this error:
for i, l in lgdf:
TypeError: cannot unpack non-iterable int object
import pandas as pd
grouped_df=pd.crosstab(df['year_month'], df['event_type'])
lgdf = list(grouped_df.max())
i = 0
for i, l in lgdf:
var[i]=l
print(var)
Although I agree with the comment of @Wups that storing the values in a list (or dictionary) would work as well, you can do the following to create variables within a for loop.
for i, l in lgdf:
exec(f'var_{i} = l')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.