import pandas as pd

# 假设我们有以下数据
data = {'id': ['A', 'A', 'B', 'B', 'B'],
        'from': ['X', 'Y', 'Z', 'X', 'Y'],
        'to': ['Y', 'Z', 'X', 'Y', 'Z']}
df = pd.DataFrame(data)

# 计算 from_counts
from_counts = df.groupby('from')['id'].nunique()

# 计算 to_counts
to_counts = df.groupby('to')['id'].nunique()

print("from_counts:")
print(from_counts)
print("to_counts:")
print(to_counts)
# 创建一个包含值的 DataFrame
df_values = pd.DataFrame({'value': list(set(df['from'].unique()) | set(df['to'].unique()))})

# 将 from_counts 和 to_counts 合并到 df_values 中
df_values['from_counts'] = df_values['value'].map(from_counts)
df_values['to_counts'] = df_values['value'].map(to_counts)
# df_values sort by value
df_values = df_values.sort_values(by='value')
# df_values['from_counts'] = df_values['from_counts'].fillna(0)
      

print(df_values)