我正在尝试将以下数据集转换为正确的格式,然后将其绘制成和弦图。
a b c d e f g h
0 1 0 0 0 0 1 0 0
1 1 0 0 0 0 0 0 0
2 1 0 1 1 1 1 1 1
3 1 0 1 1 0 1 1 1
4 1 0 0 0 0 0 0 0
5 0 1 0 0 1 1 1 1
6 1 1 0 0 1 1 1 1
7 1 1 1 1 1 1 1 1
8 1 1 0 0 1 1 0 0
9 1 1 1 0 1 0 1 0
10 1 1 1 0 1 1 0 0
11 1 0 0 0 0 1 0 0
12 1 1 1 1 1 1 1 1
13 1 1 1 1 1 1 1 1
14 0 1 1 1 1 1 1 0
结果将是显示所有变量之间可能组合的和弦图,每个流宽度是数据集中特定组合出现次数的计数-例如,上述数据集中a + b计数为7(其中是1)。
答案 0 :(得分:0)
我不知道哪个可能是最好的和弦图库,但是我可以帮您一点忙:
import pandas as pd
data = [
[1, 0, 0, 0, 0, 1, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 1, 0, 1, 1, 1],
[0, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 1, 1, 0, 0],
[1, 1, 1, 0, 1, 0, 1, 0],
[1, 1, 1, 0, 1, 1, 0, 0],
[1, 0, 0, 0, 0, 1, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 0]]
dataframe = pd.DataFrame(data, columns = ['a','b','c','d','e','f','g','h'])
def relationship (columnsList, dataframe):
result = 0
for index, row in dataframe.iterrows():
equal = True
for col in range(len(columnsList)-1):
if (equal and row[columnsList[col]] == row[columnsList[col+1]]):
equal = True
else:
equal = False
result += 1 if equal else 0
return result
>>> relationship (['a','b','d'], dataframe) # a+b+d
3
>>> relationship (['a','b','h'], dataframe) # a+b+h
4
>>> relationship (['a','b'], dataframe) # a+b
7
该图由您决定,希望对您有所帮助!