I have the following pandas dataframe:
d2 = {'col1': [0, 0, 1, 1, 2], 'col2': [10, 11, 12, 13, 14]}
df2 = pd.DataFrame(data=d2)
df2
Output:
col1 col2
0 0 10
1 0 11
2 1 12
3 1 13
4 2 14
And I need to run the following:
for i, g in df2.groupby(['col1']):
col1_val = g["col1"].iloc[0]
print(col1_val)
The original code is more complex but writing so for the purpose of illustration.
And the part for i, g in df2.groupby(['col1']):
gives the following warning:
FutureWarning: In a future version of pandas, a length 1 tuple will be returned
when iterating over a groupby with a grouper equal to a list of length 1.
Don't supply a list with a single grouper to avoid this warning.
How am I supposed to run the for loop to get rid of this warning?
This means that you should use a string instead of the list with a unique string:
for i, g in df2.groupby('col1'):
col1_val = g["col1"].iloc[0]
print(col1_val)
If you keep the original code, in the future i
will have the value (0,)
/(1,)
/(2,)
instead of 0
/1
/2