You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to use a dictionary of arbitrary length to select certain groups from a DataFrame, but if I group by a single key, it won't accept a tuple of length 1 as the key value.
Expected Output
I expect the second call to the function to return a DataFrame containing those values for which col1 == 1. (I.e. as df[df['col1'] == 1]). Perhaps my approach is ill-advised, but I have come across this problem before in situations where it was more obvious to use groupby (in the current case it might well be better to use boolean indexing).
On Apr 3, 2019, at 2:12 AM, KeithWM ***@***.***> wrote:
Code Sample, a copy-pastable example if possible
import pandas as pd
df = pd.DataFrame(data={'col1': [1, 2], 'col2': [3, 4], 'col3': [5, 6]})
def select_by_dict(dataframe, d):
return dataframe.groupby(list(d.keys())).get_group(tuple(d.values()))
select_by_dict(df, {'col1': 1, 'col2': 3})
select_by_dict(df, {'col1': 1}) # this one fails
Problem description
I would like to use a dictionary of arbitrary length to select certain groups from a DataFrame, but if I group by a single key, it won't accept a tuple of length 1 as the key value.
Expected Output
I expect the second call to the function to return a DataFrame containing those values for which col1 == 1. (I.e. as df[df['col1'] == 1]). Perhaps my approach is ill-advised, but I have come across this problem before in situations where it was more obvious to use groupby (in the current case it might well be better to use boolean indexing).
Output of pd.show_versions()
I hope you find this a useful issue.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
I did do a search for similar issues, naturally, but did not find anything. I will have a more thorough search and let you know whether I find anything.
Code Sample, a copy-pastable example if possible
Problem description
I would like to use a dictionary of arbitrary length to select certain groups from a DataFrame, but if I group by a single key, it won't accept a tuple of length 1 as the key value.
Expected Output
I expect the second call to the function to return a DataFrame containing those values for which col1 == 1. (I.e. as df[df['col1'] == 1]). Perhaps my approach is ill-advised, but I have come across this problem before in situations where it was more obvious to use groupby (in the current case it might well be better to use boolean indexing).
Output of
pd.show_versions()
I hope you find this a useful issue.
The text was updated successfully, but these errors were encountered: