0

I have created a boolean directory and applied to six different series from the same DataFrame (star_wars). It works, but curious if there is an easier way than repeating the same syntax with only changing the column headings?

seen_boolean = {'Star Wars: Episode I  The Phantom Menace': 'Yes', 'NaN': 'No', 'Star Wars: Episode II  Attack of the Clones': 'Yes', 'Star Wars: Episode III  Revenge of the Sith': 'Yes', 'Star Wars: Episode IV  A New Hope': 'Yes', 'Star Wars: Episode V The Empire Strikes Back': 'Yes', 'Star Wars: Episode VI Return of the Jedi': 'Yes'}

star_wars['seen_1'] = star_wars['seen_1'].map(seen_boolean)
star_wars['seen_2'] = star_wars['seen_2'].map(seen_boolean)
star_wars['seen_3'] = star_wars['seen_3'].map(seen_boolean)
star_wars['seen_4'] = star_wars['seen_4'].map(seen_boolean)
star_wars['seen_5'] = star_wars['seen_5'].map(seen_boolean)
star_wars['seen_6'] = star_wars['seen_6'].map(seen_boolean)
  • seems like `star_wars.notna().replace({True:'Yes',False:'No'})` – anky Apr 12 '20 at 16:27
  • It is not notna(), goal is to replace the movie names with 'Yes' unless it is NaN then replace with 'No'. Different movie names in each of the six series. – user699217 Apr 12 '20 at 16:29
  • 1
    @user699217 and that's what anky suggest, testing if not NaN, and replacing by yes if it True, or by No if untrue (and therefore a NaN). – DrGorilla.eth Apr 12 '20 at 16:37
  • 1
    Does this answer your question? [map multiple columns by a single dictionary in pandas](https://stackoverflow.com/questions/43725799/map-multiple-columns-by-a-single-dictionary-in-pandas) – DrGorilla.eth Apr 12 '20 at 16:47

1 Answers1

0

Two ways come into my mind :

Stack/unstack :

star_wars.stack().map(seen_boolean).unstack()

Loop over you columns via the default pandas iterator (but not really classy/Pandas style) :

for col in star_wars:
  star_wars[col] = star_wars[col].map(seen_boolean)
DrGorilla.eth
  • 220
  • 2
  • 11