python - How to edit the order of LastName, FirstName in pandas without losing the dataframe -
i have dataset in 2 columns have strings values "lastname, firstname". love replace them "firstname last name". these:
conductorname composername conduct_count 0 abbado, claudio berg, alban 2 1 abbado, claudio berlioz, hector 1 2 abbado, claudio bernstein, leonard 1 3 abbado, claudio brahms, johannes 2 4 abbado, claudio bruckner, anton 1
i have tried: data = [" ".join(n.split(", ")[::-1]) n in data["composername"]]
this nicely gives me list of composername lose pandas dataframe (here error ----> 1 data.head() attributeerror: 'list' object has no attribute 'head'
)
i have tried: for n in data["composername"]: " ".join(n.split(", ")[::-1])
however, not change order of lastname , firstname
i love keep pandas dataframe analysis. appreciate assistance. thank you!!
you can use vectorised str.split
split on separator, reverse list contents using slicing semantics , join again using str.join
:
in [35]: df['composerfirstlastname'] = df['composername'].str.split(', ').str[::-1].str.join(' ') df['conductorfirstlastname'] = df['conductorname'].str.split(', ').str[::-1].str.join(' ') df out[35]: composername conductorname conductor_count firstlastname \ index 0 abbado, claudio berg, alban 2 claudio abbado 1 abbado, claudio berlioz, hector 1 claudio abbado 2 abbado, claudio bernstein, leonard 1 claudio abbado 3 abbado, claudio brahms, johannes 2 claudio abbado 4 abbado, claudio bruckner, anton 1 claudio abbado composerfirstlastname conductorfirstlastname index 0 claudio abbado alban berg 1 claudio abbado hector berlioz 2 claudio abbado leonard bernstein 3 claudio abbado johannes brahms 4 claudio abbado anton bruckner
Comments
Post a Comment