Created
January 22, 2024 07:03
-
-
Save smzn/ce6c855970e9fb2079fa3f6f4f350453 to your computer and use it in GitHub Desktop.
クロス集計
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Re-importing necessary libraries and reloading the data | |
import pandas as pd | |
import seaborn as sns | |
import matplotlib.pyplot as plt | |
import itertools | |
# Function to determine if a column is binary | |
def is_binary(column): | |
return sorted(column.unique()) == [0, 1] | |
# Identifying binary columns | |
binary_columns = [col for col in data.columns if is_binary(data[col])] | |
print(binary_columns) | |
for i in range(1, len(binary_columns)): | |
binary_col1, binary_col2 = binary_columns[0], binary_columns[i] | |
# Creating a crosstab | |
ct = pd.crosstab(data[binary_col1], data[binary_col2]) | |
# Plotting the heatmap for this specific crosstab | |
plt.figure(figsize=(10, 8)) | |
sns.heatmap(ct, annot=True, fmt='d', cmap='viridis') | |
plt.title(f'Crosstab of {binary_col1} vs {binary_col2}') | |
plt.ylabel(binary_col1) | |
plt.xlabel(binary_col2) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment