Someone please help me, I'm about to give up [np.column_stack()]
python - numpy.column_stack with numeric and string arrays - Stack Overflow
[MNT] Switch from `np.column_stack()` to `np.vstack().T` for performance
ENH: Should row_stack really be deprecated?
I am doing a course on python for data science and I'm supposed to stack two columns from the dataframe using np.column_stack().
The problem is, individually the two different columns are printing the way I want them, but whenever i stack them it does some weird thing..
happy = data['happyScore']
income = data['avg_income']
print(happy.head(), income.head())
output:
0 4.350
1 4.033
2 6.574
3 7.200
4 7.284
Name: happyScore, dtype: float64
0 2096.76
1 1448.88
2 7101.12
3 19457.04
4 19917.00
Name: avg_income, dtype: float64
So these two individually are printing like I want them.
income_happy = np.column_stack((income, happy))
print(income_happy)
output:
[[2.09676000e+03 4.35000000e+00]
[1.44888000e+03 4.03300000e+00]
[7.10112000e+03 6.57400000e+00]
[1.94570400e+04 7.20000000e+00]
and so on..
What is happening because the instructor did the same thing as I did and his came out with the original values just stacked normally.
The easiest structured array approach is with the rec.fromarrays function:
In [1411]: a=np.array([3.4,3.4,6.4]); b=np.array(['holi','xlo','xlo'])
In [1412]: B = np.rec.fromarrays([a,b],names=['a','b'])
In [1413]: B
Out[1413]:
rec.array([(3.4, 'holi'), (3.4, 'xlo'), (6.4, 'xlo')],
dtype=[('a', '<f8'), ('b', '<U4')])
In [1414]: B['a']
Out[1414]: array([ 3.4, 3.4, 6.4])
In [1415]: B['b']
Out[1415]:
array(['holi', 'xlo', 'xlo'],
dtype='<U4')
Check its docs for more parameters. But it basically constructs an empty array of the correct compound dtype, and copies your arrays to the respective fields.
To store such mixed type data, most probably you would be required to store them as Object dtype arrays or use structured arrays. Going with the Object dtype arrays, we could convert either of the input arrays to an Object dtype upfront and then stack it alongside the rest of the arrays to be stacked. The rest of the arrays would be converted automatically to Object dtype to give us a stacked array of that type. Thus, we would have an implementation like so-
np.column_stack((a.astype(np.object),b))
Sample run to show how to construct a stacked array and retrieve the individual arrays back -
In [88]: a
Out[88]: array([ 3.4, 3.4, 6.4])
In [89]: b
Out[89]:
array(['holi', 'xlo', 'xlo'],
dtype='|S4')
In [90]: out = np.column_stack((a.astype(np.object),b))
In [91]: out
Out[91]:
array([[3.4, 'holi'],
[3.4, 'xlo'],
[6.4, 'xlo']], dtype=object)
In [92]: out[:,0].astype(float)
Out[92]: array([ 3.4, 3.4, 6.4])
In [93]: out[:,1].astype(str)
Out[93]:
array(['holi', 'xlo', 'xlo'],
dtype='|S4')