Home > Software design >  ValueError: Data must be 1-dimensional......verify_integrity
ValueError: Data must be 1-dimensional......verify_integrity

Time:03-13

Bonjour,

I don't understand why this issue occurs.

print("p.shape= ", p.shape)
print("dfmj_dates['deces'].shape = ",dfmj_dates['deces'].shape)
cross_dfmj = pd.crosstab(p, dfmj_dates['deces'])

That produces:

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

From me, I suspect issue comes from the difference between (683, 1) and (683,). I tried something like p.flatten(order = 'C') to get (683,) but pd.DataFrame(dfmj_dates['deces']) too. That failed. Do you have any idea? Regards, Atapalou

CodePudding user response:

Try to squeeze p:

cross_dfmj = pd.crosstab(p.squeeze(), dfmj_dates['deces'])

Example:

p = np.random.random((5, 1))
p.shape
# (5, 1)

p.squeeze().shape
# (5,)

CodePudding user response:

print(p.head(30))
print(df.head(30))

that produces

    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

CodePudding user response:

I tried that:

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)

that produces

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

But issue is the same......

  • Related