[英]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:这是该列的数据字典:
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.