将列拆分为Pandas中的两个新列

时间:2017-12-07 12:45:09

标签: python pandas dataframe

我有一个包含4列的数据框,其值如下:

- name: Loop all the present users
  debug: msg={{ item }}
  when: "{{ item.state == 'present' }}"
  with_items: "{{ iam_users }}"
  tags: always
  register: present_users

- set_fact:
    iam_present_users: "{{ present_users.results }}"
  tags: always

- name: Show only present users, ideally
  debug: msg="{{ iam_present_users }}"
  tags: always

- name: Manage AWS IAM Roles
  iam_role:
    name: "{{ item.name }}"
    assume_role_policy_document: "{{ lookup('template', policies_path + '/assume-role/' + item.assume_role_policy_document + '.json') }}"
    state: "{{ item.state }}"
    managed_policy: "{{ item.managed_policy }}"
  when: "{{ item.managed_policy is defined and iam_present_users is defined }}"
  with_items: "{{ iam_roles }}"
  tags: manage_roles

我需要通过分拆&#39 ;;'来制作每个列的两列。 当我尝试这个时:

value_1
over 1 - 42 -> take this ; over 3 - 4
over 3 - 26 -> take this ; over 3 - 45 
over 5 - 25 -> take this ; over 2 - 80 

或者这个:

s = df['value_1'].apply(lambda x: x.split(';'))
df['value_left'] = s.apply(lambda x: x[0])
df['value_right'] = s.apply(lambda x: x[1])

我得到了相同的f['new_value1'] = df['value_1'] df['value_1_right'] = None df['value_1_right'].update(df.new_band_bandw_1.apply(lambda x: x.str.split(';')[1] if len(x.str.split()) == 2 else None)) 。问题是,这些值是否可能存在于某种列表中?

欢迎任何类型的解决方案。感谢

2 个答案:

答案 0 :(得分:2)

您需要split

df[['value_left','value_right']] = df['value_1'].str.split(';', expand=True)
print (df)

                                  value_1                 value_left  \
0   over 1 - 42 -> take this ; over 3 - 4  over 1 - 42 -> take this    
1  over 3 - 26 -> take this ; over 3 - 45  over 3 - 26 -> take this    
2  over 5 - 25 -> take this ; over 2 - 80  over 5 - 25 -> take this    

    value_right  
0    over 3 - 4  
1   over 3 - 45  
2   over 2 - 80  

多个;的示例 - 可以指定哪个;用于拆分:

df = pd.DataFrame({
    'value_1': ['a;r;e','b;r','c;g;t;e']
})
print (df)

   value_1
0    a;r;e
1      b;r
2  c;g;t;e

df[['value_left','value_right']] = df['value_1'].str.split(';', expand=True, n=1)
print (df)
   value_1 value_left value_right
0    a;r;e          a         r;e
1      b;r          b           r
2  c;g;t;e          c       g;t;e

答案 1 :(得分:2)

字符串partition的另一种方式,即

df[['val1','val2']] = df[0].str.partition(';').iloc[:,0::2]

                                     0                       val1          val2
0    over 1 - 42 -> take this ; over 3 - 4  over 1 - 42 -> take this     over 3 - 4  
1   over 3 - 26 -> take this ; over 3 - 45  over 3 - 26 -> take this     over 3 - 45
2  over 5 - 25 -> take this ; over; 2 - 80  over 5 - 25 -> take this     over; 2 - 80