简体   繁体   English

将pandas dataframe列数据分离成新的新列

[英]Separating pandas dataframe column data into new new columns

my data column combines information on two axes: wealth and life stage.我的数据栏结合了两个轴上的信息:财富和生活阶段。 I need to break up the two-digit codes by their 'tens'-place and 'ones'-place digits into two new ordinal variables.我需要将两位数代码的“十”位和“个”位数字分解为两个新的序数变量。 How would I go about separating and re-encoding this data?我 go 如何分离和重新编码这些数据?

This is the data dictionary for the column:这是该列的数据字典:

  • -1: unknown -1:未知
  • 11: Wealthy Households - Pre-Family Couples & Singles 11: 富裕家庭 - 未婚夫妇和单身人士
  • 12: Wealthy Households - Young Couples With Children 12: 富裕家庭 - 有孩子的年轻夫妇
  • 13: Wealthy Households - Families With School Age Children 13: 富裕家庭 - 有学龄儿童的家庭
  • 14: Wealthy Households - Older Families & Mature Couples 14: 富裕家庭 - 年长的家庭和成熟的夫妻
  • 15: Wealthy Households - Elders In Retirement 15: 富裕家庭 - 退休的长者
  • 21: Prosperous Households - Pre-Family Couples & Singles 21: 富贵之家 - 未婚夫妇及单身人士
  • 22: Prosperous Households - Young Couples With Children 22: 富裕家庭 - 年轻夫妇与孩子
  • 23: Prosperous Households - Families With School Age Children 23: 富裕家庭 - 有学龄儿童的家庭
  • 24: Prosperous Households - Older Families & Mature Couples 24: 富裕家庭 - 年长的家庭和成熟的夫妻
  • 25: Prosperous Households - Elders In Retirement 25: 富贵之家 - 退休的长者
  • 31: Comfortable Households - Pre-Family Couples & Singles 31: 舒适的家庭 - 婚前夫妇和单身人士
  • 32: Comfortable Households - Young Couples With Children 32: 舒适的家庭 - 有孩子的年轻夫妇
  • 33: Comfortable Households - Families With School Age Children 33: 舒适的家庭 - 有学龄儿童的家庭
  • 34: Comfortable Households - Older Families & Mature Couples 34: 舒适的家庭 - 年长的家庭和成熟的夫妻
  • 35: Comfortable Households - Elders In Retirement 35: 舒适的家庭 - 退休的老人
  • 41: Less Affluent Households - Pre-Family Couples & Singles 41: 较不富裕的家庭 - 婚前夫妇和单身人士
  • 42: Less Affluent Households - Young Couples With Children 42: 较不富裕的家庭 - 有孩子的年轻夫妇
  • 43: Less Affluent Households - Families With School Age Children 43: 较不富裕的家庭 - 有学龄儿童的家庭
  • 44: Less Affluent Households - Older Families & Mature Couples 44: 较不富裕的家庭 - 年长的家庭和成熟的夫妻
  • 45: Less Affluent Households - Elders In Retirement 45: 较不富裕的家庭 - 退休的长者
  • 51: Poorer Households - Pre-Family Couples & Singles 51: 较贫穷的家庭 - 未婚夫妇和单身人士
  • 52: Poorer Households - Young Couples With Children 52: 较贫穷的家庭 - 有孩子的年轻夫妇
  • 53: Poorer Households - Families With School Age Children 53: 较贫穷的家庭 - 有学龄儿童的家庭
  • 54: Poorer Households - Older Families & Mature Couples 54: 较贫穷的家庭 - 年长的家庭和成熟的夫妻
  • 55: Poorer Households - Elders In Retirement 55: 较贫穷的家庭 - 退休的老人

I have tried np.where() and created conditions:我试过 np.where() 并创造了条件:

I have used.map():我用过.map():

data2.map({11:'Wealthy', 12:'Wealthy', 13:'Wealthy', 14:'Wealthy', 15:'Wealthy',
#          21:'Prosperous', 22:'Prosperous', 23:'Prosperous', 24:'Prosperous', 25:'Prosperous',
#          31:'Comfortable', 32:'Comfortable', 33:'Comfortable', 34:'Comfortable', 35:'Comfortable',
#          41:'Less_Afflutent', 42:'Less_Afflutent', 43:'Less_Afflutent', 44:'Less_Afflutent', 45:'Less_Afflutent',
#          51:'Poorer', 52:'Poorer', 53:'Poorer', 54:'Poorer', 55:'Poorer'})

I've tried a couple other things as well that didn't seem worth even posting.我还尝试了其他一些似乎不值得发布的东西。 I'm new and still learning but I've never come across aa problem like this before and I've done SO much googling我是新手,仍在学习,但我以前从未遇到过这样的问题,而且我已经做了很多谷歌搜索

If I understand the question correctly, you want to split your current status column into two new columns and then make mappings for each of those.如果我正确理解了这个问题,您想将当前状态列拆分为两个新列,然后为每个列进行映射。

# Assuming your current column is called 'status'
df['tens_place'] = df['status']//10 # whole number of division, no remainder, gives -1 for -1
df['ones_place'] = df['status']%10 # just remainder, gives 9 for -1
tens_map = tens_map = {1:'Wealthy Households',2:'Prosperous households',
    3:'Comfortable Households',4:'Less Affluent households' ,5:'Poorer households',
    -1:'unknown'}
ones_map = ... # same sorta thing
df['wealth_status'] = df['tens_place'].map(tens_map)
df['family_status'] = df['ones_place'].map(ones_map)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM