Comments (5)
@gwerbin Can you share a reproducible example?
from pandas.
Seems to me this is P.join(Q, how="right")
, but as @samukweku said - please provide a reproducible example with the input and desired output.
from pandas.
It's not exactly the same as the right join because I'm not interested in any of the columns from Q
, only subsetting indices. But I realize now that you could write it like this, which is a lot tidier than what I was doing before:
P.join(Q.loc[:, []], how="right")
I also had different behavior in mind for if there were duplicates in Q.index
than what right join offers, but that wasn't part of my original example.
Maybe this is a good enough recipe to warrant not adding new functionality to the Pandas interface, but it's not obvious and IMO would look like opaque magic to a typical non-expert user.
The non-duplicate case
P = pd.DataFrame(
data=[
(1, 9, "u", -1.70),
(1, 9, "v", -1.75),
(2, 8, "u", -1.60),
(2, 8, "v", -1.65),
(1, 8, "u", -1.50),
(1, 8, "v", -1.55),
(2, 7, "u", -1.40),
(2, 7, "v", -1.45),
],
columns=["a", "b", "c", "x"],
).set_index(["a", "b", "c"])
Q = pd.DataFrame(
data=[
(1, 9, 2.5),
(2, 7, 3.5),
],
columns=["a", "b", "y"],
).set_index(["a", "b"])
expected = pd.DataFrame(
data=[
(1, 9, "u", -1.70),
(1, 9, "v", -1.75),
(2, 7, "u", -1.40),
(2, 7, "v", -1.45),
],
columns=["a", "b", "c", "x"],
).set_index(["a", "b", "c"])
pd.testing.assert_frame_equal(
P.join(Q.loc[:, []], how="right"),
expected,
)
The duplicate case
I didn't want to complicate things by including this example, but this is where what I want diverges from a typical right join.
Inputs:
P = pd.DataFrame(
data=[
(1, 9, "u", -1.70),
(1, 9, "v", -1.75),
(2, 8, "u", -1.60),
(2, 8, "v", -1.65),
(1, 8, "u", -1.50),
(1, 8, "v", -1.55),
(2, 7, "u", -1.40),
(2, 7, "v", -1.45),
],
columns=["a", "b", "c", "x"],
).set_index(["a", "b", "c"])
Q = pd.DataFrame(
data=[
(1, 9, 2.5),
(2, 7, 3.5),
(2, 7, 4.5),
],
columns=["a", "b", "y"],
).set_index(["a", "b"])
expected = pd.DataFrame(
data=[
(1, 9, "u", -1.70),
(1, 9, "v", -1.75),
(2, 7, "u", -1.40),
(2, 7, "v", -1.45),
],
columns=["a", "b", "c", "x"],
).set_index(["a", "b", "c"])
pd.testing.assert_frame_equal(
P.join(Q.loc[~Q.index.duplicated(keep="first"), []], how="right"),
expected,
)
Prior art
Note that Polars supports how="semi"
join in the polars.DataFrame.join
method: https://docs.pola.rs/py-polars/html/reference/dataframe/api/polars.DataFrame.join.html
They also support the similarly-useful how="anti"
join, which was also not part of the original intended scope, but would IMO be useful in Pandas for similar reasons.
from pandas.
But I realize now that you could write it like this, which is a lot tidier than what I was doing before:
P.join(Q.loc[:, []], how="right")
It can be slightly simpler with P.join(Q[[]], how="right")
.
Maybe this is a good enough recipe to warrant not adding new functionality to the Pandas interface, but it's not obvious and IMO would look like opaque magic to a typical non-expert user.
Adding a new method/arguments to pandas for cases like this is not sustainable in my opinion.
from pandas.
P.join(Q.loc[~Q.index.duplicated(keep="first"), []], how="right")
Sorry - I missed this but my position is the same. pandas provides all the tools for you to accomplish the computation in a reasonable line of code.
from pandas.
Related Issues (20)
- BUG: rounding datetime in series is broken HOT 1
- BUG: DataFrame to JSON failed when it with UUID HOT 4
- ENH: merge_asof with "how” functionality
- ENH: is it possible to save a reference to a method depending on the version HOT 3
- ValueError: Usecols do not match columns, columns expected but not found: ['Col3', 'Col1'] HOT 6
- ENH: Query function to have a star * for all columns
- BUG: read_html returns empty list HOT 3
- database is locked
- BUG: Enlarging multilevel index fails if one or more level keys are None HOT 2
- BUG: `.dt.microsecond` for pyarrow-backed Series returns 0
- VOTE: Voting issue for PDEP-14: Dedicated string data type for pandas 3.0 HOT 11
- DEPR: `keep_default_dates` and `convert_dates` in `read_json`
- ENH: Add Option to Include Array Offset as MultiIndex Level in `explode()` HOT 6
- DOC: Sort the pandas API reference navbar in alphabetical order HOT 1
- ENH: In pd.cut(), allow bins='auto' (leveraging `np.histogram_bin_edges`) HOT 1
- QST: Critical Vulnerability detection in pandas Library by Fortify due to read_pickle function HOT 3
- BUG:
- BUG: incorrect cummax() results when applied to PyArrow dtypes HOT 1
- ENH: Add Dict type to pd.concat objs HOT 5
- BUG: pd.api.extensions.take does not accept NumpyExtensionArray HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pandas.