0

If I use np.concatenate to join this 1x5 array

array([True , False, False, True , True  ])

with this 1x5 array

array([4.753, 1.202, 2.296, 1.668, 3.35  ])

The booleans are changed to integers:

array([[1.   , 0.   , 0.   , 1.   , 1.   ],
       [4.753, 1.202, 2.296, 1.668, 3.35 ]])

Why? how can I concatenate them without affecting the booleans?

Ideally, the output can be a pandas DataFrame

0

2 Answers 2

1

Something like this?

In [445]: import numpy as np
     ...: import pandas as pd

In [446]: data = {'Booleans': np.array([True, False, False, True, True]), 
     ...:          'Floats': np.array([4.753, 1.202, 2.296, 1.668, 3.35])}

In [447]: df = pd.DataFrame(data)

In [448]: df
Out[448]: 
   Booleans  Floats
0      True   4.753
1     False   1.202
2     False   2.296
3      True   1.668
4      True   3.350

You may find this link useful: Different ways to create Pandas Dataframe.

Sign up to request clarification or add additional context in comments.

Comments

1

One way is to set dtype of the individual arrays as object (you just need one of them to be object type to result in an object type array)

a = np.array([[True , False, False, True , True]], dtype=object)
b = np.array([[4.753, 1.202, 2.296, 1.668, 3.35]])

np.concatenate([a,b])
array([[True, False, False, True, True],
       [4.753, 1.202, 2.296, 1.668, 3.35]], dtype=object)

You can use arr.astype() to convert each array to object type after defining them as well.

a.astype(object)
#array([[True, False, False, True, True]], dtype=object)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.