0

My question is similar to this question How to solve the AttributeError:'list' object has no attribute 'astype'? however, I'm trying to create a series of strings, in order to plot in graph.

 feature_names = df.iloc[:,maxIndex].columns
 newMaxName.append(feature_names)
 newMaxName = np.array(newMaxName, dtype =np.str)
 newMaxName

so it gives me output as

array(['shar', 'dec', 'intel', 'like', 'amb', 'met', 'pid', 'prob_o', 'pf_o_sin', 'pf_o_att', 'pf_o_sha', 'pf_o_fun', 'age_o', 'pf_o_amb', 'pf_o_int', 'attr_o', 'attr', 'attr2_1', 'sinc', 'shar1_1', 'shar_o', 'amb_o', 'intel1_1', 'attr1_1', 'fun_o'], dtype='<U8')

I want them to be string how can I convert them?

3
  • Do you want to extract list of strings? .tolist() would do that. These are already strings in UTF-8 Commented Dec 30, 2020 at 12:26
  • 2
    dtype='<U8' is the string datatype Commented Dec 30, 2020 at 12:27
  • stackoverflow.com/q/16037824/14291629 might be of interest. Commented Dec 30, 2020 at 12:44

1 Answer 1

1

You already did convert it to a string datatype, the default unicode string datatype:

>>> arr
array(['shar', 'dec', 'intel', 'like', 'amb', 'met', 'pid', 'prob_o',
       'pf_o_sin', 'pf_o_att', 'pf_o_sha', 'pf_o_fun', 'age_o',
       'pf_o_amb', 'pf_o_int', 'attr_o', 'attr', 'attr2_1', 'sinc',
       'shar1_1', 'shar_o', 'amb_o', 'intel1_1', 'attr1_1', 'fun_o'],
      dtype='<U8')

If you want byte-strins, be specific:

>>> arr.astype(np.dtype('S'))
array([b'shar', b'dec', b'intel', b'like', b'amb', b'met', b'pid',
       b'prob_o', b'pf_o_sin', b'pf_o_att', b'pf_o_sha', b'pf_o_fun',
       b'age_o', b'pf_o_amb', b'pf_o_int', b'attr_o', b'attr', b'attr2_1',
       b'sinc', b'shar1_1', b'shar_o', b'amb_o', b'intel1_1', b'attr1_1',
       b'fun_o'], dtype='|S8')

Note, byte-strings may be preferable, for memory purposes:

>>> arr.nbytes
800
>>> arr.astype(np.dtype('S')).nbytes
200

However

If your purpose is to plot, just working with object is almost certainly ok. Note, prior to version 1.00 pandas does not use any string-specific types, object was the string type.

If you are working with pandas you may want:

>>> import pandas as pd
>>> pd.__version__
'1.1.1'
>>> s = pd.Series(['abc','def', 'ghi'])
>>> s
0    abc
1    def
2    ghi
dtype: object
>>> s.astype(pd.StringDtype())
0    abc
1    def
2    ghi
dtype: string
Sign up to request clarification or add additional context in comments.

2 Comments

Yes ı want to plot in graph as yticks, like this ` plt.yticks(y_p,newMaxName) `, in the way arr.astype(np.dtype("S)) it works but the it work in horizontal , but it should be vertical since ı put them in graph, and the last part you said for pd.Series, this strings is the columns of the pandas
@Razbolt it works the same for a column in a dataframe. Your other problem seems totally unrelated to this. Again, almost certainly, you didn't have to do any conversion to begin with.

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.