我正在嘗試旋轉以下資料框:

但是當我這樣做時,一切都會回傳 NaN 值,例如:

我查看了堆疊溢位的所有內容,但找不到任何答案。我正在嘗試使用這三個變數制作熱圖。非常感謝任何和所有幫助!
這是一些偽代碼
x = df['x']
y = df['y']
z = df['z']
data = pd.DataFrame({'X': X, 'Y': Y, 'Z': Z})
#then when I try to pivot:
pd.pivot_table(data.dropna(),
index=['X'],
columns='Y', values = 'Z')
uj5u.com熱心網友回復:
這是正常的行為。
假設這個較小的資料框:
df = pd.DataFrame({'x': [1, 2], 'y': [3, 4], 'z': [5, 6]})
print(df)
# Output:
x y z
0 1 3 5
1 2 4 6
現在,旋轉您的資料框:
df1 = df.pivot(index='x', columns='y', values='y')
print(df1)
# Output:
y 3 4
x
1 5.0 NaN # NaN because there is no value for z when (x, y) -> (1, 4)
2 NaN 6.0 # NaN because there is no value for z when (x, y) -> (2, 3)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/369428.html
