-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpybnesianCPT_to_df.py
39 lines (28 loc) · 1.14 KB
/
pybnesianCPT_to_df.py
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
39
import pandas as pd
def from_CPT_to_df(data_string):
"""
Converts a Conditional Probability Table (CPT) represented as a string into a pandas DataFrame.
Parameters:
- data_string (str): The string representation of the CPT.
Returns:
- df (pandas.DataFrame): The CPT data as a DataFrame.
"""
# Split the string into lines
lines = data_string.strip().split('\n')
# Remove the horizontal line separators
lines = [line for line in lines if not line.startswith('+')]
# Find the index where the column names are defined
header_separator = 3
# Extract the column names
columns = [column.strip() for column in lines[header_separator-1].split('|') if column.strip()]
# Find the index where the data rows start
data_start = header_separator
# Extract the data rows
data_rows = [line.split('|')[1:-1] for line in lines[data_start:]]
# Clean up and format the data
data = []
for row in data_rows:
clean_row = [item.strip() for item in row]
data.append(clean_row)
df = pd.DataFrame(data, columns=columns)
return df