-
-
Notifications
You must be signed in to change notification settings - Fork 18k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request for some kind of named arguments loc #11373
Comments
we've had discussions #4036 and #11267 Here is an example which is pretty straightforward to do:
xray calls this Here's a quick and dirty impl
|
Thank you for the quick reply. The |
@BigSkylie the issues with actually using a function, and the reason am not a big fan of is that this is a function call, and therefor cannot be used as an lvalue. So the second we add:
people will want
The alternatives are allow dictionaries in eg.
and/or the syntax I described above
having pandas 'figure out' which axis you mean via the index name is laudable and would be sovled by dict access |
@jreback Only some minutes after my previous post, I joined the group of disappointed people who want
I now must completely agree with you, The syntax |
@BigSkylie yes It think accepting a dict is on the roadmap, interested in implementing? (not too hard actually), as it is just a pre-processing transformation step |
Well, one thing we'll need to figure out here is whether dictionary indexing refers to levels of the multi-index or column/index names. Does |
@jreback As soon as I see a Pandas conform solution, I at least will give it a try. Is making changes to |
what is a 'Pandas conform solution'? follow how part of the challenge is ironing out the API to make it clean semantically. So need some tests cases for as much as possible. |
@jreback 'Pandas conform' was meant to be related to Pandas similarly to how 'Pythonic' is related to Python. A clean solution, which keeps the user experience coherent all over Pandas. |
+1, FWIW |
+1 on interest for such a feature The index/column question came up. Why not just handle it in the standard .loc way? df.loc[{index1:'A', index2: slice(None), index3: [1,3,5], index4: range(1,10,2)}, :] I tried taking a look at the code to see how .loc[] actually works, but it is above my head. If anyone would care to give me a roadmap about the inner workings, I would be more than willing to take another look. Here is my current solution, by way of chance. It is a bit hacky, but it works perfectly for my needs. My experimental data has files with titles of the form
|
I wrote a wrapper around https://gist.github.com/MPvHarmelen/3a3db1b83c0ac82eb41eacbe0cee2d0c As of now it does getting and setting on any dimension, but not deleting (as it didn't seem like >>> df = pd.DataFrame(
... [(x, -x) for x in range(1, 5)],
... index=pd.MultiIndex.from_product(
... [['a', 'b'], [1, 2]],
... names=['letter', 'number'],
... )
... )
>>> df
0 1
letter number
a 1 1 -1
2 2 -2
b 1 3 -3
2 4 -4
>>> df.loc_by_level_name[{'number': 2}]
0 1
letter number
a 2 2 -2
b 2 4 -4
>>> df.loc_by_level_name(axis=0)[{'number': 1}]
0 1
letter number
a 1 1 -1
b 1 3 -3
>>> df.loc_by_level_name[{'number': 1}, 1] = 12
>>> df
0 1
letter number
a 1 1 12
2 2 -2
b 1 3 12
2 4 -4
>>> fd = df.transpose()
>>> fd
letter a b
number 1 2 1 2
0 1 2 3 4
1 12 -2 12 -4
>>> fd.loc_by_level_name[:, {'letter': 'a'}]
letter a
number 1 2
0 1 2
1 12 -2
>>> fd.loc_by_level_name(axis=1)[{'letter': 'b'}]
letter b
number 1 2
0 3 4
1 12 -4 |
Thanks for the request, but it appears this feature request hasn't been picked up by the community or core team in years so closing for now |
Please quickly recall how awesome named arguments for Python are and then read through the following idea:
In case of identical or almost identical values of
index
andcolumns
, it can be annoying to remember the assignment of the two data-axes to index and columns respectively. This could be solved by assigning names toindex
andcolumns
, e.g.index_name=y
andcolumns_name=x
, and hence being able to retrieve values viapd.DataFrame.name_loc(x=10, y=20)
(syntax not final).I have no idea, if such a feature can be implemented with reasonable effort. I do however believe, that it would highly increase the usability of DataFrames for certain usecases.
The text was updated successfully, but these errors were encountered: