Ritu Singh
To choose columns and rows based on a keyword in a specific column (let's call it "column1") in a DataFrame in Python, you can use the Pandas library. Here are the steps:
1. Import the necessary libraries:
import pandas as pd
2. Load or create your DataFrame. You can either read data from a file or create a DataFrame from scratch. Here's an example of creating a DataFrame from a dictionary:
data = {
"column1": ["apple", "banana", "cherry", "date"],
"column2": [10, 20, 30, 40],
"column3": [100, 200, 300, 400]
}
df = pd.DataFrame(data)
3. Filter rows based on the keyword in "column1":
keyword = "banana" # Replace with your desired keyword
filtered_df = df[df["column1"] == keyword]
This code creates a new DataFrame `filtered_df` that contains only the rows where "column1" is equal to the specified keyword.
4. Optionally, choose specific columns:
If you also want to select specific columns from the filtered DataFrame, you can do so by providing a list of column names:
selected_columns = ["column2", "column3"] # Replace with your desired column names
final_df = filtered_df[selected_columns]
Now, `final_df` contains only the rows where "column1" is equal to the specified keyword, and it includes only the selected columns.
Here's the complete code with all the steps:
import pandas as pd
data = {
"column1": ["apple", "banana", "cherry", "date"],
"column2": [10, 20, 30, 40],
"column3": [100, 200, 300, 400]
}
df = pd.DataFrame(data)
keyword = "banana"
filtered_df = df[df["column1"] == keyword]
selected_columns = ["column2", "column3"]
final_df = filtered_df[selected_columns]
print(final_df)
This code will print the DataFrame `final_df`, which contains only the rows where "column1" is "banana," and it includes only "column2" and "column3."
Suggested blogs:
>How to save python yaml and load a nested class?
>What makes Python 'flow' with HTML nicely as compared to PHP?