2

i have this code:

dfx = pd.read_csv('hasil_processing_auto.csv', encoding='unicode_escape')
dy = dfx.loc[:,'polarity']
X = dy.values

print(X)

and this is the output i get:

['[0]' '[0]' '[0]' '[0]' '[0]' '[0]' '[0]' '[0]' '[0]' '[0]' '[0]' '[0]'
 '[1]' '[0]' '[0]' '[0]' '[0]' '[0]' '[0]' '[1]']

i want it to be just like [0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1]

the csv is like this:

    tweets                                              label   polarity
0   i love myself kuliah tugas tumpuk sapu ngepel ...   Netral  [0]
1   suogh online online kuliah                          Netral  [0]
2   bisnis sekolah kuliah ngantor online shop prod...   Netral  [0]
3   tunggu surat putus rektor surat putus dekan la...   Netral  [0]
4   kuliah online bosan                                 Netral  [0]
.....
2
  • Your list is evaluated to object type when reading. To read it properly as valid python list try ast.literal_eval Commented Nov 29, 2020 at 16:28
  • The read_csv loads those [0] column values as strings. This sort of thing often occurs when you save a dataframe as csv, and the original frame had lists (or array) elements. csv is best used when the dataframe has simple column elements - numbers and strings. Commented Nov 29, 2020 at 18:01

1 Answer 1

4

The easiest way is to strip the column as such:

'[0]'[1:-1] --> '0'

If you are new to Python, you can treat a string as an iterable so you can subset it by [start:end]

dfx['polarity'] = dfx['polarity'].apply(lambda x: x[1:-1])
Sign up to request clarification or add additional context in comments.

3 Comments

got this output ['' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '']
sorry got to be -1
I think he means -1

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.