我的表格中有一栏包含"女性与男性的比例"例如:45:55我希望将女性的值(左侧,例如45)放在一列中,将男性的值(右侧,例如55)放入一个新列中单独的列。
我的问题是我需要在Pandas(使用iPython笔记本)中执行此操作。我一直在互联网上寻找一个不使用系列的解决方案 - 它似乎并不适合我。有什么建议?
以下是该列和行的截图:
非常感谢任何帮助!
答案 0 :(得分:0)
我确信有一种更优雅的方式可以做到这一点;然而,这是一个快速而肮脏的选择:
#Create a function to split the values
def splitter(ratio):
try: #Try to split the values
return ratio.split(" : ")[0], ratio.split(" : ")[1]
except AttributeError: #return None for NaN's
return None, None
#apply the function to the column
df.loc[:, 'MaleNumerator'], df.loc[:, 'FemaleDenominator'] = zip(*df.male_female_Ratio.apply(splitter))