-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumn_selection.py
More file actions
38 lines (27 loc) · 1.13 KB
/
column_selection.py
File metadata and controls
38 lines (27 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 2 17:46:40 2025
@author: shagh
"""
import pandas as pd
def select_final_columns(df, DesiredColumns):
"""
Selects only the specified columns from the DataFrame.
Args:
df (pd.DataFrame): The input DataFrame.
DesiredColumns (list): A list of column names to keep in the final DataFrame.
Returns:
pd.DataFrame: A DataFrame containing only the selected columns.
"""
# Get a list of existing columns in df
existing_columns = df.columns.tolist()
# Find columns that are available in the DataFrame
available_columns = [col for col in DesiredColumns if col in existing_columns]
# Warn if any DesiredColumns are missing
missing_columns = set(DesiredColumns) - set(available_columns)
if missing_columns:
print(f"Warning: The following columns were not found in the DataFrame and will be ignored: {missing_columns}")
# Select only the available columns
df = df[available_columns]
print(f"Final DataFrame contains {len(df.columns)} columns: {available_columns}")
return df