calling list() method over pandas dataframe single row raises an error. For example,
d = {'col1': ['a', 'b', 'a'], 'col2': ['c', 'd', 'e']}
df = pd.DataFrame(data=d)
df
Now, the below is fine
list(df.loc[df.col1 == 'a']['col2'].values.squeeze())
but,
list(df.loc[df.col1 == 'b']['col2'].values.squeeze())
raises:
TypeError: iteration over a 0-d array
How to address this issue?
You can use pd.Series.tolist()
here.
df.loc[df.col1 == 'a','col2'].tolist()
df.loc[df.col1 == 'b','col2'].tolist()
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.