在 Pandas 中不使用 Series 分析数据时如何处理值 "ratio"
How to deal with the values "ratio" when analysing data without using Series in Pandas
我的 table 中有一列包含一个 "female to male ratio" 例如:45 : 55 我想为女性输入值(在左侧,例如 45 ) 在一列中,男性的值(在右侧,例如 55)在一个新的单独列中。
我的问题是我需要在 Pandas 中执行此操作(使用 iPython 笔记本)。我一直在互联网上搜索不使用 Series 的解决方案 - 它似乎对我不起作用。有什么建议么?
这是该列和行的屏幕截图:
如有任何帮助,我们将不胜感激!
我确信有更优雅的方式来做到这一点;然而,这里有一个快速而肮脏的选择:
#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))
我的 table 中有一列包含一个 "female to male ratio" 例如:45 : 55 我想为女性输入值(在左侧,例如 45 ) 在一列中,男性的值(在右侧,例如 55)在一个新的单独列中。
我的问题是我需要在 Pandas 中执行此操作(使用 iPython 笔记本)。我一直在互联网上搜索不使用 Series 的解决方案 - 它似乎对我不起作用。有什么建议么?
这是该列和行的屏幕截图:
如有任何帮助,我们将不胜感激!
我确信有更优雅的方式来做到这一点;然而,这里有一个快速而肮脏的选择:
#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))