你好,
我不明白為什么會出現這個問題。
print("p.shape= ", p.shape)
print("dfmj_dates['deces'].shape = ",dfmj_dates['deces'].shape)
cross_dfmj = pd.crosstab(p, dfmj_dates['deces'])
這會產生:
p.shape= (683, 1)
dfmj_dates['deces'].shape = (683,)
----> 3 cross_dfmj = pd.crosstab(p, dfmj_dates['deces'])
--> 654 df = DataFrame(data, index=common_idx)
--> 614 mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)
--> 589 val = sanitize_array(
--> 576 subarr = _sanitize_ndim(subarr, data, dtype, index, allow_2d=allow_2d)
--> 627 raise ValueError("Data must be 1-dimensional")
ValueError: Data must be 1-dimensional
在我看來,我懷疑問題來自 (683, 1) 和 (683,) 之間的差異。我嘗試了類似 p.flatten(order = 'C') 的方法來獲得 (683,) 但 pd.DataFrame(dfmj_dates['deces']) 也是如此。那失敗了。你有什么主意嗎?問候, 阿塔帕盧
uj5u.com熱心網友回復:
嘗試squeezep:
cross_dfmj = pd.crosstab(p.squeeze(), dfmj_dates['deces'])
例子:
p = np.random.random((5, 1))
p.shape
# (5, 1)
p.squeeze().shape
# (5,)
uj5u.com熱心網友回復:
我試過了:
print("p.shape= ", p.shape)
print("dfmj_dates['deces'].shape = ",dfmj_dates['deces'].shape)
df = pd.DataFrame(dfmj_dates['deces'], index = p.index)
print("df.shape,", df.shape)
print(p.head(3))
print(df.head(3))
cross_dfmj = pd.crosstab(p, df)
產生
p.shape= (683, 1)
dfmj_dates['deces'].shape = (683,)
df.shape, (683, 1)
week
0 8
1 8
2 8
deces
0 0
1 1
2 0
但是問題是一樣的......
uj5u.com熱心網友回復:
print(p.head(30))
print(df.head(30))
產生
week
0 8
1 8
2 8
3 9
4 9
5 9
6 9
7 9
8 9
9 9
10 10
11 10
12 10
13 10
14 10
15 10
16 10
17 11
18 11
19 11
20 11
21 11
22 11
23 11
24 12
25 12
26 12
27 12
28 12
29 12
deces
0 0
1 1
2 0
3 0
4 0
5 1
6 0
7 0
8 0
9 0
10 1
11 1
12 0
13 3
14 4
15 5
16 3
17 11
18 3
19 15
20 13
21 18
22 12
23 36
24 21
25 27
26 69
27 128
28 78
29 112
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/441978.html
