-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
Closed
Labels
Milestone
Description
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 failsProblem 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()
Details
INSTALLED VERSIONS ------------------ commit: None python: 3.6.1.final.0 python-bits: 64 OS: Darwin OS-release: 18.2.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: None LOCALE: None.None pandas: 0.23.3 pytest: 3.3.1 pip: 18.1 setuptools: 36.6.0 Cython: 0.29.5 numpy: 1.14.5 scipy: 1.1.0 pyarrow: None xarray: None IPython: 5.4.0 sphinx: None patsy: 0.4.1 dateutil: 2.6.1 pytz: 2017.3 blosc: None bottleneck: None tables: 3.4.2 numexpr: 2.6.4 feather: None matplotlib: 2.0.2 openpyxl: 2.5.4 xlrd: 1.1.0 xlwt: None xlsxwriter: None lxml: None bs4: 4.7.1 html5lib: 0.9999999 sqlalchemy: None pymysql: None psycopg2: None jinja2: 2.10 s3fs: None fastparquet: None pandas_gbq: None pandas_datareader: NoneI hope you find this a useful issue.
kevin1kevin1k