Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

python - Why does numpy array of booleans change to integers if concatenated with another array?

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

question from:https://stackoverflow.com/questions/65621390/why-does-numpy-array-of-booleans-change-to-integers-if-concatenated-with-another

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...