根据相同条件在数据框中创建多个列

时间:2018-10-27 02:27:22

标签: python

我是Python的新手,我试图一次基于一个if语句条件在数据框中创建多个列。我创建了一个类似于下面的定义函数,为每个if语句返回2个值,第二个if语句嵌套在第一个if语句中,然后将该函数应用于数据框以创建4个新列。显然,这是不正确的,希望获得一些帮助或其他选择以达到相同的结果。谢谢!

[['1', 'My Favorite Murder with Karen Kilgari...', 'Comedy', '-'], ['2', 'True Crime Garage', 'Society & Culture', '+3'], ['3', 'Serial', 'Society & Culture', '-'], ['4', 'The Joe Rogan Experience', 'Comedy', '-2'], ['5', 'Fantasy Football Today Podcast', 'Sports', '+1'], ['6', 'This American Life', 'Society & Culture', '+1'], ['7', 'Dr. Death', 'Science & Medicine', '-3'], ['8', 'The Rachel Maddow Show', 'News & Politics', '-'], ['9', 'The Daily', 'News & Politics', '-'], ['10', 'Sword and Scale', 'Society & Culture', '+4'], ['11', 'Last Podcast On The Left', 'Pop Culture, TV & Film', '-1'], ['12', 'Freakonomics Radio', 'Society & Culture', '-'], ['13', 'Radiolab', 'Science & Medicine', '+4'], ['14', 'Casefile True Crime', 'Pop Culture, TV & Film', '-3'], ['15', 'Pod Save America', 'News & Politics', '-2'], ['16', 'Spittin Chiclets', 'Sports', '+2'], ['17', 'Stuff You Should Know', 'Education', '-1'], ['18', 'Planet Money', 'Business', '-3'], ['19', 'Pardon My Take', 'Sports', '+4'], ['20', 'The Dream', 'Society & Culture', '+9'], ['21', 'Lore', 'Society & Culture', '+14'], ['22', 'Up and Vanished', 'News & Politics', '-1'], ['23', 'Hidden Brain', 'Science & Medicine', '-1'], ['24', '99% Invisible', 'Society & Culture', '-4'], ['25', 'Fresh Air', 'Society & Culture', '-1'], ['26', 'TED Radio Hour', 'Society & Culture', '+1'], ['27', "Wait Wait...Don't Tell Me!", 'Games & Hobbies', '-8'], ['28', 'The Moth', 'Storytelling', '+2'], ['29', 'Up First', 'News & Politics', '+3'], ['30', 'Criminal', 'Society & Culture', '+9'], ['31', 'The Bill Simmons Podcast', 'Sports', '+2'], ['32', 'Wolverine: The Long Night', 'Society & Culture', '+4'], ['33', 'Someone Knows Something', 'News & Politics', '+61'], ['34', 'Reply All', 'Technology', '-8'], ['35', 'The Generation Why Podcast', 'Society & Culture', '+3'], ['36', 'Armchair Expert with Dax Shepard', 'Comedy', '+1'], ['37', 'Today, Explained', 'News & Politics', '-3'], ['38', 'Dirty John', 'Society & Culture', '-10'], ['39', 'Morning Joe', 'News & Politics', '+4'], ['40', 'The Ben Shapiro Show', 'News & Politics', '+4'], ['41', 'Real Time with Bill Maher', 'Comedy', '-10'], ['42', 'Sold In America', 'Society & Culture', '+4'], ['43', 'NPR News Now', 'News & Politics', '+2'], ['44', 'Slow Burn', 'Society & Culture', '-19'], ['45', 'How I Built This with Guy Raz', 'Business', '-5'], ['46', 'WTF with Marc Maron Podcast', 'Comedy', '+2'], ['47', 'NPR Politics Podcast', 'News & Politics', '-'], ['48', "And That's Why We Drink", 'Society & Culture', '-6'], ['49', 'The Tim Ferriss Show', 'Lifestyle & Health', '+4'], ['50', 'The Indicator from Planet Money', 'Business', '-'], ['51', 'Sleep With Me', 'Society & Culture', '-10'], ['52', 'Snap Judgment', 'Storytelling', '-1'], ['53', 'Small Town Murder', 'Society & Culture', '+2'], ['54', 'Waking Up with Sam Harris', 'Society & Culture', '+35'], ['55', 'Gladiator: Aaron Hernandez and Footba...', 'News & Politics', '+3813'], ['56', 'Global News', 'News & Politics', '-'], ['57', 'Fantasy Footballers - Fantasy Footbal...', 'Sports', '-3'], ['58', 'Stuff You Missed in History Class', 'Society & Culture', '+1'], ['59', "Vox's The Weeds", 'News & Politics', '+40'], ['60', 'The Ezra Klein Show', 'News & Politics', '+36'], ['61', 'Heavyweight', 'Society & Culture', '+4'], ['62', 'Juicy Scoop with Heather McDonald', 'Comedy', '-1'], ['63', 'Mad Money w/ Jim Cramer', 'Business', '-3'], ['64', "Your Mom's House with Christina P. an...", 'Comedy', '+5'], ['65', 'FiveThirtyEight Politics', 'News & Politics', '-8'], ['66', 'StarTalk Radio', 'Science & Medicine', '+7'], ['67', 'How Did This Get Made?', 'Comedy', '-3'], ['68', 'Unobscured', 'Society & Culture', '+23'], ['69', 'Oprah’s SuperSoul Conversations', 'Society & Culture', '+15'], ['70', 'The NoSleep Podcast', 'Pop Culture, TV & Film', '+12'], ['71', 'Serial Killers', 'Society & Culture', '-22'], ['72', 'MeatEater Podcast', 'Sports', '+4'], ['73', 'Marketplace with Kai Ryssdal', 'News & Politics', '-5'], ['74', "The Church of What's Happening Now: W...", 'Comedy', '+31'], ['75', 'Fantasy Focus Football', 'Sports', '-17'], ['76', 'The Dave Ramsey Show', 'Business', '-2'], ['77', 'Invisibilia', 'Storytelling', '+3'], ['78', 'Limetown', 'Pop Culture, TV & Film', '+342'], ['79', 'Political Gabfest', 'News & Politics', '-8'], ['80', 'My Dad Wrote A Porno', 'Comedy', '+6'], ['81', 'Armstrong and Getty', 'News & Politics', '-9'], ['82', 'Stay Tuned with Preet', 'News & Politics', '+3'], ['83', 'Lovett or Leave It', 'News & Politics', '-17'], ['84', 'Wild Thing', 'Science & Medicine', '+289'], ['85', "Stuff They Don't Want You To Know", 'Pop Culture, TV & Film', '+22'], ['86', 'In the Dark', 'News & Politics', '-8'], ['87', 'The Fighter & The Kid', 'Sports', '+10'], ['88', 'Guys We F****d', 'Pop Culture, TV & Film', '-7'], ['89', 'Ground Zero Media', 'Society & Culture', '+21'], ['90', 'Pop Culture Happy Hour', 'Pop Culture, TV & Film', '+8'], ['91', 'DISGRACELAND', 'Music & Commentary', '+18'], ['92', 'The Vanished Podcast', 'Society & Culture', '-9'], ['93', 'Savage Lovecast', 'Lifestyle & Health', '-6'], ['94', 'Snap Judgment Presents: Spooked', 'Pop Culture, TV & Film', '+9'], ['95', 'Science Vs', 'Science & Medicine', '-25'], ['96', 'The Last Word with Lawrence O’Donnell', 'Everything', '-29'], ['97', 'The Glenn Beck Program', 'News & Politics', '-22'], ['98', 'True Crime All The Time', 'Society & Culture', '-5'], ['99', 'My Brother, My Brother And Me', 'Comedy', '+1'], ['100', 'The Ringer NBA Show', 'Sports', '+18']]

并产生一个像这样的数据框...

def group(df):
  if df['a'] == 'one':
    low1 = 10
    high1 = 60

    if df['b'] == 'two':
    low2 = 20
    high2 = 70

    return low1, high1, low2, high2

df['low1', 'high1', 'low2', 'high2'] = df.apply(group, axis=1)

1 个答案:

答案 0 :(得分:0)

如@Lucefer的注释中所述,如果第一个条件为true,则它将永远不会达到第二个条件。因此,交换if语句可能对您有用。

def group(df):
     if df['a'] == 'one' & df['b'] == 'two':
        low2 = 20
        high2 = 70
        return low2, high2

    if df['a'] == 'one':
        low1 = 10
        high1 = 60
        return low1, high1