dimanche 27 août 2017

Why does combining if elif else, str.contains, and .any() in a pandas dataframe give me uniform result?

I try to make new lable tor every lines in a dataset based on text that contained in previous column, heres my code

import pandas as pd
import numpy as np
data = pd.read_excel('C:\\Users\\data july 2017.xlsx')
#data['Description'].fillna('')
pawoon = data[data['Description'].str.contains('Pawoon')]
pawoon['time'] = "0"
if pawoon.Description.str.contains('1 Bulan').any():
    pawoon['time'] = 1
elif pawoon.Description.str.contains('3 Bulan').any():
    pawoon['time'] = 3
elif pawoon.Description.str.contains('6 bulan').any():
    pawoon['time'] = 6
elif pawoon.Description.str.contains('1 Tahun').any():
    pawoon['time'] = 1
else:
    pawoon['time'] = 0

Here's the partial result that i've got, when I print partial result of the code above

Pawoon POS Software (1 Bulan)   1
Software Pawoon (6 bulan)       1
Pawoon POS Software (1 Tahun)   1
Pawoon POS Software (3 Bulan)   1
Pawoon POS Software (1 Bulan)   1

The result that I expected is showed below (based on if elif else statement that I mention in my code)

Pawoon POS Software (1 Bulan)   1
Software Pawoon (6 bulan)       6
Pawoon POS Software (1 Tahun)   12
Pawoon POS Software (3 Bulan)   3
Pawoon POS Software (1 Bulan)   1

Why does it's happen? How to get my desired result? I guess .any() should be change, but if I change to .all every result uniformly 0

Aucun commentaire:

Enregistrer un commentaire