我想使用python將項目列表轉換爲方形矩陣格式。 我嘗試了熊貓的pivot_table,但沒有奏效。如何將列表轉置爲方形矩陣
這裏是我的代碼,輸入是一個兩列的csv文件
with open(path_to_file,"r") as f:
reader = csv.reader(f,delimiter = ',')
data = list(reader)
row_count=len(data)
print(row_count - 1)
df = pd.read_csv(path_to_file)
groups = df.groupby(['transmitter chan', 'receiver chan'])
max_for_AS = defaultdict(int)
df = df.assign(ID = [0 + i for i in xrange(len(df))])
print(df)
for g in groups:
transmitter, count = g[0][0], len(g[1])
max_for_AS[ transmitter ] = max(max_for_AS[transmitter], count)
for g in groups:
transmitter, receiver, count = g[0][0], g[0][1], len(g[1])
if count == max_for_AS[ transmitter ]:
dataFinal = "{} , {} , {}".format(transmitter, receiver, count)
print(dataFinal)
數據:
V1 V2 count
0 A R 1
1 Z T 4
2 E B 9
3 R O 8
4 T M 7
5 Y K 5
6 B I 6
7 T Z 2
8 A O 7
9 Y B 8
請張貼您的數據,而不是您的數據圖片。另外,請您嘗試迄今爲止的代碼。 – James
是這些numpy數組嗎?熊貓數據框?你可以發佈你試過的代碼嗎? –