如何在数据表中另外两列值相等时获取指定列的对应值?
Hey there! Let's figure out how to get the user value where the center and right columns have matching values. Based on your sample data, here are a couple of common approaches depending on the tool you're using:
If you're querying a database table, a simple SELECT statement with a WHERE clause will do the trick. Just filter rows where center equals right, then select the user column:
SELECT user FROM your_table_name WHERE center = right;
In your sample data, this query will return mary—since her center (1000) and right (1000) values match—while Jane's mismatched values get excluded.
If you're working with a pandas DataFrame, here's how you can achieve the same result:
First, let's set up your sample data:
import pandas as pd # Create the sample DataFrame data = { 'user': ['mary', 'jane'], 'center': [1000, 9999], 'right': [1000, 2222] } df = pd.DataFrame(data)
Then filter the rows where center equals right and extract the user value:
# Get all matching users (as a list) matching_users = df[df['center'] == df['right']]['user'].tolist() # If you only need the first match (like in your sample) print(matching_users[0]) # Output: mary
This will give you exactly the user you're looking for when the two columns have equal values.
内容的提问来源于stack exchange,提问作者nerd

