Skip to content

Instantly share code, notes, and snippets.

@smzn
Created January 22, 2024 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smzn/ce6c855970e9fb2079fa3f6f4f350453 to your computer and use it in GitHub Desktop.
Save smzn/ce6c855970e9fb2079fa3f6f4f350453 to your computer and use it in GitHub Desktop.
クロス集計
# 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