vlookup in python
时间: 2023-09-20 10:15:06 浏览: 104
In Python, you can use the 'pandas' library to perform a similar operation to Excel's VLOOKUP function. The 'pandas' library provides the 'merge' function, which can be used to combine two dataframes based on a common column.
Here is an example of how you can perform a VLOOKUP-like operation in Python using pandas:
```python
import pandas as pd
# Create two dataframes
df1 = pd.DataFrame({'Code': ['A', 'B', 'C', 'D'],
'Value': [10, 20, 30, 40})
df2 = pd.DataFrame({'Code': ['B', 'D', 'E', 'F'],
'Description': ['Description1', 'Description2', 'Description3', 'Description4']})
# Perform VLOOKUP
merged_df = pd.merge(df1, df2, on='Code', how='left')
print(merged_df)
```
This code will merge the two dataframes based on the 'Code' column, similar to how VLOOKUP works in Excel. The resulting dataframe will contain columns from both dataframes, with matching values filled in and non-matching values filled with NaN.
You can adjust the 'how' parameter to change the type of join ('left', 'right', 'inner', or 'outer') based on your specific requirements.
I hope this helps! Let me know if you have any further questions.
阅读全文