Is It Possible To Have A Single Unique Index For Multiple Rows In Pandas?
Is it possible to have a single unique index for multiple rows in pandas? Example: index country value 1 NL 'hi' 2 NL 'wet' 3 SWE '4' 4 SWE 'maybe' So, in this example I cannot s
Solution 1:
You can absolutely set column country
as the index in pandas. Indexes do not have to be unique. You could then grab all the rows for the value NL
with the .loc
operator.
df = df.set_index('country')
df.loc['NL']
From your comments below try this first with your original dataframe (no setting index).
df.groupby('country')['value'].apply(list).to_json())
Output
{"NL":["'hi'","'wet'"],"SWE":["'4'","'maybe'"]}
Post a Comment for "Is It Possible To Have A Single Unique Index For Multiple Rows In Pandas?"