Skip to content

Commit 4cf29d0

Browse files
2 parents 09e640e + cdac439 commit 4cf29d0

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

statwrap/fpp.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,23 @@ def histogram(*data_args, class_intervals=None, bins=None, density=True, xlim=No
539539

540540
def fpp_setup():
541541
apply_pd_changes()
542+
543+
def contingency_table(data, column_1, column_2):
544+
"""
545+
Generates a contingency table from pandas DataFrame from two specified columns
546+
547+
Parameters:
548+
data: The DataFrame containing the data (define df = example_DataFrame)
549+
column_1: Title of the first column.
550+
column_2: Title of the second column.
551+
552+
Returns:
553+
pd.DataFrame: Contingency Table.
554+
"""
555+
if column_1 not in data.columns:
556+
raise ValueError(f"Column_1 '{column_1}' is not in DataFrame.")
557+
if column_2 not in data.columns:
558+
raise ValueError(f"Column_2 '{column_2}' is not in DataFrame.")
559+
560+
contingency_table = pd.crosstab(data[column_1], data[column_2])
561+
return contingency_table

0 commit comments

Comments
 (0)