text_plain_output_1.png
import pandas as pd

battles_df = pd.read_csv('../input/battles.csv')

battles_df.drop('year', axis=1, inplace=True)

battles_df.drop(['name', 'battle_number', 'note'], axis=1, inplace=True)

pattern = '[a-z][0-9]'
test = battles_df[~battles_df.attacker_2.isnull()].attacker_2.replace(pattern, 1, regex=True)

battles_df.attacker_2.fillna(0, inplace=True)
battles_df.attacker_3.fillna(0, inplace=True)
battles_df.attacker_4.fillna(0, inplace=True)

def find_attacker_allies(my_data):
    col2 = my_data['attacker_2']
    col3 = my_data['attacker_3']
    col4 = my_data['attacker_4']
    number = 0
    if col2 != 0:
        number = number + 1
    if col3 != 0:
        number = number + 1
    if col4 != 0:
        number = number + 1
    return number
battles_df['attacker_allies'] = battles_df.apply(find_attacker_allies, axis=1)

battles_df.drop(['attacker_2', 'attacker_3', 'attacker_4'], axis=1, inplace=True)

battles_df.defender_2.fillna(0, inplace=True)
battles_df.defender_3.fillna(0, inplace=True)
battles_df.defender_4.fillna(0, inplace=True)

def find_defender_allies(my_data):
    col2 = my_data['defender_2']
    col3 = my_data['defender_3']
    col4 = my_data['defender_4']
    number = 0
    if col2 != 0:
        number = number + 1
    if col3 != 0:
        number = number + 1
    if col4 != 0:
        number = number + 1
    return number
battles_df['defender_allies'] = battles_df.apply(find_defender_allies, axis=1)
battles_df.drop(['defender_2', 'defender_3', 'defender_4'], axis=1, inplace=True)

battles_df.head()