应用拆分函数到多个列

huangapple go评论68阅读模式
英文:

Applying a split function to multiple columns

问题

我有一个包含特殊字符的数据框。我想用一个函数删除这些列中的特殊字符。

import pandas as pd
import numpy as np

# 40行完整数据框
data = {'Region': ['East', 'South', 'South', 'North', 'North', 'South', 'East', 'North', 'North', 'West ', 'West ', 'South', 'North', 'East', 'South', 'South', 'North', 'East', 'South', 'North', 'East', 'North', 'West ', 'North', 'East', 'South', 'West ', 'North', 'South', 'West ', 'West ', 'South', 'South', 'South', 'East', 'East', 'South', 'West ', 'East', 'South'],
        'District': ['Kono', 'Bo', 'Pujehun', 'Port Loko', 'Koinadugu', 'Bo', 'Kono', 'Koinadugu', 'Koinadugu', 'Western Urban', 'Western Rural ', 'Pujehun', 'Bombali', 'Kono', 'Pujehun', 'Pujehun', 'Port Loko', 'Kenema', 'Bo', 'Koinadugu', 'Kono', 'Koinadugu', 'Western Urban', 'Koinadugu', 'Kailahun', 'Bo', 'Western Urban', 'Bombali', 'Bo', 'Western Rural ', 'Western Urban', 'Pujehun', 'Pujehun', 'Moyamba', 'Kono', 'Kenema', 'Bo', 'Western Urban', 'Kailahun', 'Bo'],
        'Chiefdom': ['Nimiyama', 'Bo Town', 'Barri', 'Lokomasama', 'Nieni', 'Bo Town', 'Gbense', 'Diang', 'Dembelia-Sinkunia', 'West III', 'Waterloo Rural', 'Makpele', 'Sella Limba', 'Nimikoro', np.nan, 'Pejeh(Futa peje', 'Lokomasama', 'Kandu Leppiama', 'Bo Town', 'Sengbe', 'Gbane Kandor', 'Nieni', 'West III', 'Nieni', 'Mandu', 'Valunia', 'East III', 'Sella Limba', 'Bo Town', 'Mountain Rural', 'West I', 'Kpaka', 'Makpele', 'Ribbi', 'Nimikoro', 'Gaura', 'Bo Town', 'West II', 'Mandu', 'Bo Town'],
        'Section': ['Sewafeh', 'Second Bongay street coronation field', 'Sembehun Barri', 'Raka', 'Bandakarifya', 'J 11 Nikibu Section', 'Hill station', 'Kondaibaia', 'Gbindi Central', 'Aberdeen', 'Bass Street', 'Dombu', 'Kathantha Yimbor', 'jiama', 'Mandaima', 'Njagbema', 'mapang', 'Baoma oil mill', 'Gbonda Road Coronation Field', 'Dankawalie', 'Fanda Viĺlage', 'Kumala', 'Lumley', 'Kumala', 'Mobai', 'Baomahun', 'Jalloh terrace', 'Kabba Ferry', 'Tenp.nant Street Niagboima', 'Regent', 'Siaka Steven Stadium', 'Soso', 'Jorporwahun', 'foryah village', 'Yendema', 'Perrie', 'Forth Street', 'Assesion Twon', 'Yoyah', 'Cee Line'],
        'wp Name': ['sokoya', 'More Rogers compound', 'Sembehun drilled well', 'community  well', 'Bandakarifya Health facility hand pump', 'Mr  Bangle', 'Kamara family', 'Hand dug well', 'Samba Water well', '9 Kimbima road', 'indian mac', 'Guboi Community', 'Wcsl pri School well kathantha.', 'public stand pipe', 'Mandaima', 'Njagbema Community', 'school well', 'CHC', 'More Sarah water point', 'mamayeria water well', 'Strime', 'Kumala community tap', '16 E Freetown Road', 'Kumala community tap', 'Tobu', 'Baomahun community water', 'Amidu Bah water point 12 city road', 'Kabba ferry com well 3', 'SALWACO   Water Point', '7 Gloucester Road Regent', 'Stadium Hostels', 'Sowa compand', 'Kallonla', 'foryah community well', 'Hand dug well', 'Lanssana Jumu', 'Forth  Street

<details>
<summary>英文:</summary>

I have a dataframe with some columns with special character. I want to remove those characters in the columns with one function to iterate in each column

```python
import pandas as pd
import numpy as np

# 40 rows of complete dataframe
data = {&#39;Region&#39;: [&#39;East&#39;, &#39;South&#39;, &#39;South&#39;, &#39;North&#39;, &#39;North&#39;, &#39;South&#39;, &#39;East&#39;, &#39;North&#39;, &#39;North&#39;, &#39;West &#39;, &#39;West &#39;, &#39;South&#39;, &#39;North&#39;, &#39;East&#39;, &#39;South&#39;, &#39;South&#39;, &#39;North&#39;, &#39;East&#39;, &#39;South&#39;, &#39;North&#39;, &#39;East&#39;, &#39;North&#39;, &#39;West &#39;, &#39;North&#39;, &#39;East&#39;, &#39;South&#39;, &#39;West &#39;, &#39;North&#39;, &#39;South&#39;, &#39;West &#39;, &#39;West &#39;, &#39;South&#39;, &#39;South&#39;, &#39;South&#39;, &#39;East&#39;, &#39;East&#39;, &#39;South&#39;, &#39;West &#39;, &#39;East&#39;, &#39;South&#39;],
        &#39;District&#39;: [&#39;Kono&#39;, &#39;Bo&#39;, &#39;Pujehun&#39;, &#39;Port Loko&#39;, &#39;Koinadugu&#39;, &#39;Bo&#39;, &#39;Kono&#39;, &#39;Koinadugu&#39;, &#39;Koinadugu&#39;, &#39;Western Urban&#39;, &#39;Western Rural &#39;, &#39;Pujehun&#39;, &#39;Bombali&#39;, &#39;Kono&#39;, &#39;Pujehun&#39;, &#39;Pujehun&#39;, &#39;Port Loko&#39;, &#39;Kenema&#39;, &#39;Bo&#39;, &#39;Koinadugu&#39;, &#39;Kono&#39;, &#39;Koinadugu&#39;, &#39;Western Urban&#39;, &#39;Koinadugu&#39;, &#39;Kailahun&#39;, &#39;Bo&#39;, &#39;Western Urban&#39;, &#39;Bombali&#39;, &#39;Bo&#39;, &#39;Western Rural &#39;, &#39;Western Urban&#39;, &#39;Pujehun&#39;, &#39;Pujehun&#39;, &#39;Moyamba&#39;, &#39;Kono&#39;, &#39;Kenema&#39;, &#39;Bo&#39;, &#39;Western Urban&#39;, &#39;Kailahun&#39;, &#39;Bo&#39;],
        &#39;Chiefdom&#39;: [&#39;Nimiyama&#39;, &#39;Bo Town&#39;, &#39;Barri&#39;, &#39;Lokomasama&#39;, &#39;Nieni&#39;, &#39;Bo Town&#39;, &#39;Gbense&#39;, &#39;Diang&#39;, &#39;Dembelia-Sinkunia&#39;, &#39;West III&#39;, &#39;Waterloo Rural&#39;, &#39;Makpele&#39;, &#39;Sella Limba&#39;, &#39;Nimikoro&#39;, np.nan, &#39;Pejeh(Futa peje&#39;, &#39;Lokomasama&#39;, &#39;Kandu Leppiama&#39;, &#39;Bo Town&#39;, &#39;Sengbe&#39;, &#39;Gbane Kandor&#39;, &#39;Nieni&#39;, &#39;West III&#39;, &#39;Nieni&#39;, &#39;Mandu&#39;, &#39;Valunia&#39;, &#39;East III&#39;, &#39;Sella Limba&#39;, &#39;Bo Town&#39;, &#39;Mountain Rural&#39;, &#39;West I&#39;, &#39;Kpaka&#39;, &#39;Makpele&#39;, &#39;Ribbi&#39;, &#39;Nimikoro&#39;, &#39;Gaura&#39;, &#39;Bo Town&#39;, &#39;West II&#39;, &#39;Mandu&#39;, &#39;Bo Town&#39;],
        &#39;Section&#39;: [&#39;Sewafeh&#39;, &#39;Second Bongay street coronation field&#39;, &#39;Sembehun Barri&#39;, &#39;Raka&#39;, &#39;Bandakarifya&#39;, &#39;J 11 Nikibu Section&#39;, &#39;Hill station&#39;, &#39;Kondaibaia&#39;, &#39;Gbindi Central&#39;, &#39;Aberdeen&#39;, &#39;Bass Street&#39;, &#39;Dombu&#39;, &#39;Kathantha Yimbor&#39;, &#39;jiama&#39;, &#39;Mandaima&#39;, &#39;Njagbema&#39;, &#39;mapang&#39;, &#39;Baoma oil mill&#39;, &#39;Gbonda Road Coronation Field&#39;, &#39;Dankawalie&#39;, &#39;Fanda Viĺlage&#39;, &#39;Kumala&#39;, &#39;Lumley&#39;, &#39;Kumala&#39;, &#39;Mobai&#39;, &#39;Baomahun&#39;, &#39;Jalloh terrace&#39;, &#39;Kabba Ferry&#39;, &#39;Tenp.nant Street Niagboima&#39;, &#39;Regent&#39;, &#39;Siaka Steven Stadium&#39;, &#39;Soso&#39;, &#39;Jorporwahun&#39;, &#39;foryah village&#39;, &#39;Yendema&#39;, &#39;Perrie&#39;, &#39;Forth Street&#39;, &#39;Assesion Twon&#39;, &#39;Yoyah&#39;, &#39;Cee Line&#39;],
        &#39;wp Name&#39;: [&#39;sokoya&#39;, &#39;More Rogers compound&#39;, &#39;Sembehun drilled well&#39;, &#39;community  well&#39;, &#39;Bandakarifya Health facility hand pump&#39;, &#39;Mr  Bangle&#39;, &#39;Kamara family&#39;, &#39;Hand dug well&#39;, &#39;Samba Water well&#39;, &#39;9 Kimbima road&#39;, &#39;indian mac&#39;, &#39;Guboi Community&#39;, &#39;Wcsl pri School well kathantha.&#39;, &#39;public stand pipe&#39;, &#39;Mandaima&#39;, &#39;Njagbema Community&#39;, &#39;school well&#39;, &#39;CHC&#39;, &#39;More Sarah water point&#39;, &#39;mamayeria water well&#39;, &#39;Strime&#39;, &#39;Kumala community tap&#39;, &#39;16 E Freetown Road&#39;, &#39;Kumala community tap&#39;, &#39;Tobu&#39;, &#39;Baomahun community water&#39;, &#39;Amidu Bah water point 12 city road&#39;, &#39;Kabba ferry com well 3&#39;, &#39;SALWACO   Water Point&#39;, &#39;7 Gloucester Road Regent&#39;, &#39;Stadium Hostels&#39;, &#39;Sowa compand&#39;, &#39;Kallonla&#39;, &#39;foryah community well&#39;, &#39;Hand dug well&#39;, &#39;Lanssana Jumu&#39;, &#39;Forth  Street&#39;, &#39;Gray Bush&#39;, &#39;community pump&#39;, &#39;Private&#39;],
        &#39;Latitude&#39;: [8.58331956, 7.954980701482942, 7.40037027, 8.707599, 9.26926281, 7.959563252331055, 8.64424692, 9.38030499, 9.91149193, 8.49867403, 8.32211437, 7.31525401, 9.54705837071811, 8.54967284, 7.44251431, 7.50650933, 8.778933309645701, 8.04202, 7.956939396996258, 9.63100657, 8.64097544, 9.06444497, 8.45597167, 9.06357503, 7.99293476, 8.41437496755645, 8.464235217224767, 9.586626399662924, 7.954810813602998, 8.43581747, 8.48045449, 7.29593482, 7.34045448, 8.291899626644375, 8.57198629, 7.660193333333332, 7.955800507520509, 8.481692669842559, 7.93007759, 7.952044236023938],
        &#39;Longitude&#39;: [-11.22175896, -11.738578683547583, -11.38590836, -12.99610243, -11.19311269, -11.749480176861558, -10.95233617, -11.56538161, -11.4472028, -13.28776409, -13.07541178, -11.26317706, -12.170908329699612, -11.08732622, -11.74650559, -11.52460214, -13.092156324868172, -11.328288333333337, -11.740514057032058, -11.32949058, -10.59901501, -11.40249037, -13.27166235, -11.40196698, -10.75225434, -11.669409575710674, -13.181634554478194, -12.217858620215289, -11.749735856785582, -13.21830899, -13.24944949, -11.65502583, -11.33420895, -12.77962831263424, -11.09309991, -11.079631666666668, -11.74833729662934, -13.24936134294358, -10.76663342, -11.733189998375863],
        &#39;Elevation&#39;: [328.0, 150.3, 80.0, 58.0, 505.0, 132.3, 421.0, 394.0, 388.0, 51.0, 54.8, 172.0, 129.3, 413.0, 94.0, 113.0, 31.5, 141.6, 193.3, 457.0, 264.0, 441.0, 58.0, 428.0, 223.6, 147.5, 120.3, 42.7, 124.2, 409.0, 31.0, 57.0, 127.0, -74.9, 432.0, 103.8, 152.5, 87.3, 253.0, 231.3],
        &#39;Type of water point&#39;: [&#39;11:Dam/pan (runoff harvesting)&#39;, &#39;9:Unprotected dug well&#39;, &#39;3:Tube well or borehole&#39;, &#39;2:Protected dug well&#39;, &#39;2:Protected dug well&#39;, &#39;9:Unprotected dug well&#39;, &#39;2:Protected dug well&#39;, &#39;2:Protected dug well&#39;, &#39;2:Protected dug well&#39;, &#39;7:Piped water into dwelling/plot/yard&#39;, &#39;2:Protected dug well&#39;, &#39;2:Protected dug well&#39;, &#39;2:Protected dug well&#39;, &#39;6:Public tap/standpipe (stand-alone or water kiosk&#39;, &#39;1:Protected spring&#39;, &#39;2:Protected dug well&#39;, &#39;2:Protected dug well&#39;, &#39;2:Protected dug well&#39;, &#39;9:Unprotected dug well&#39;, &#39;2:Protected dug well&#39;, &#39;12:Surface water (lake/river/stream)&#39;, &#39;6:Public tap/standpipe (stand-alone or water kiosk&#39;, &#39;7:Piped water into dwelling/plot/yard&#39;, &#39;6:Public tap/standpipe (stand-alone or water kiosk&#39;, &#39;6:Public tap/standpipe (stand-alone or water kiosk&#39;, &#39;9:Unprotected dug well&#39;, &#39;2:Protected dug well&#39;, &#39;2:Protected dug well&#39;, &#39;2:Protected dug well&#39;, &#39;7:Piped water into dwelling/plot/yard&#39;, &#39;6:Public tap/standpipe (stand-alone or water kiosk&#39;, &#39;2:Protected dug well&#39;, &#39;2:Protected dug well&#39;, &#39;2:Protected dug well&#39;, &#39;3:Tube well or borehole&#39;, &#39;6:Public tap/standpipe (stand-alone or water kiosk&#39;, &#39;9:Unprotected dug well&#39;, &#39;6:Public tap/standpipe (stand-alone or water kiosk&#39;, &#39;2:Protected dug well&#39;, &#39;9:Unprotected dug well&#39;],
        &#39;Water point Functionality&#39;: [&#39;1:Yes – Functional (and in use)&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;4:No - Broken down&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;4:No - Broken down&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;3:Yes - But damaged&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;2:Yes – Functional (but not in use)&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;4:No - Broken down&#39;, &#39;4:No - Broken down&#39;, &#39;4:No - Broken down&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;6:No - Under rehabilitation&#39;, &#39;4:No - Broken down&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;4:No - Broken down&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;4:No - Broken down&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;2:Yes – Functional (but not in use)&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;3:Yes - But damaged&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;3:Yes - But damaged&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;1:Yes – Functional (and in use)&#39;, &#39;1:Yes – Functional (and in use)&#39;],
        &#39;Water Sustainability&#39;: [&#39;Seasonal&#39;, &#39;Seasonal&#39;, &#39;Seasonal&#39;, &#39;Seasonal&#39;, &#39;Seasonal&#39;, &#39;Seasonal&#39;, &#39;Always water&#39;, &#39;Always water&#39;, &#39;Always water&#39;, &#39;Always water&#39;, &#39;Seasonal&#39;, &#39;Seasonal&#39;, &#39;Seasonal&#39;, &#39;Always water&#39;, &#39;Dry always / Never water&#39;, &#39;Always water&#39;, &#39;Seasonal&#39;, &#39;Seasonal&#39;, &#39;Seasonal&#39;, &#39;Seasonal&#39;, &#39;Seasonal&#39;, &#39;Always water&#39;, &#39;Always water&#39;, &#39;Always water&#39;, &#39;Seasonal&#39;, &#39;Always water&#39;, &#39;Always water&#39;, &#39;Dry always / Never water&#39;, &#39;Seasonal&#39;, &#39;Seasonal&#39;, &#39;Always water&#39;, &#39;Seasonal&#39;, &#39;Seasonal&#39;, &#39;Always water&#39;, &#39;Always water&#39;, &#39;Seasonal&#39;, &#39;Seasonal&#39;, &#39;Always water&#39;, &#39;Seasonal&#39;, &#39;Seasonal&#39;],
        &#39;WP_suitability&#39;: [&#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;2:No&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;2:No&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;2:No&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;2:No&#39;, &#39;1:Yes&#39;, &#39;2:No&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;9999:Uknown&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;],
        &#39;Year of construction&#39;: [1990, 2003, 2016, 1984, 2010, 2006, 2011, 2013, 1998, 2011, 2013, 2005, 2013, 1986, 2000, 2008, 2013, 2015, 2008, 1985, 1970, 1991, 2013, 1991, 1982, 2010, 2000, 2005, 2009, 2010, 1979, 2014, 2014, 1985, 2009, 1988, 2006, 2015, 2004, 2013],
        &#39;Ownership&#39;: [&#39;1:Community&#39;, &#39;8:Private Individual&#39;, &#39;1:Community&#39;, &#39;1:Community&#39;, &#39;6:Health Facility&#39;, &#39;8:Private Individual&#39;, &#39;8:Private Individual&#39;, &#39;1:Community&#39;, &#39;8:Private Individual&#39;, &#39;8:Private Individual&#39;, &#39;8:Private Individual&#39;, &#39;1:Community&#39;, &#39;5:School&#39;, &#39;1:Community&#39;, &#39;5:School&#39;, &#39;1:Community&#39;, &#39;5:School&#39;, &#39;6:Health Facility&#39;, &#39;8:Private Individual&#39;, &#39;1:Community&#39;, &#39;1:Community&#39;, &#39;1:Community&#39;, &#39;8:Private Individual&#39;, &#39;1:Community&#39;, &#39;1:Community&#39;, &#39;8:Private Individual&#39;, &#39;8:Private Individual&#39;, &#39;1:Community&#39;, &#39;8:Private Individual&#39;, &#39;8:Private Individual&#39;, &#39;3:GUMA&#39;, &#39;1:Community&#39;, &#39;1:Community&#39;, &#39;1:Community&#39;, &#39;1:Community&#39;, &#39;1:Community&#39;, &#39;8:Private Individual&#39;, &#39;1:Community&#39;, &#39;1:Community&#39;, &#39;8:Private Individual&#39;],
        &#39;Maintenance in charge&#39;: [&#39;1:WASH management committee&#39;, &#39;3:Privat owner&#39;, &#39;2:Other community group&#39;, &#39;2:Other community group&#39;, &#39;2:Other community group&#39;, &#39;3:Privat owner&#39;, &#39;3:Privat owner&#39;, &#39;1:WASH management committee&#39;, &#39;3:Privat owner&#39;, &#39;3:Privat owner&#39;, &#39;3:Privat owner&#39;, &#39;2:Other community group&#39;, &#39;10:NGO&#39;, &#39;1:WASH management committee&#39;, &#39;10:NGO&#39;, &#39;2:Other community group&#39;, &#39;1:WASH management committee&#39;, &#39;7:Health Facility&#39;, &#39;3:Privat owner&#39;, &#39;1:WASH management committee&#39;, &#39;11:No management&#39;, &#39;2:Other community group&#39;, &#39;3:Privat owner&#39;, &#39;2:Other community group&#39;, &#39;2:Other community group&#39;, &#39;3:Privat owner&#39;, &#39;3:Privat owner&#39;, &#39;10:NGO&#39;, &#39;3:Privat owner&#39;, &#39;3:Privat owner&#39;, &#39;5:GUMA&#39;, &#39;1:WASH management committee&#39;, &#39;10:NGO&#39;, &#39;2:Other community group&#39;, &#39;2:Other community group&#39;, &#39;1:WASH management committee&#39;, &#39;3:Privat owner&#39;, &#39;1:WASH management committee&#39;, &#39;1:WASH management committee&#39;, &#39;3:Privat owner&#39;],
        &#39;WASH management committee?&#39;: [&#39;1:Yes&#39;, &#39;2:No&#39;, &#39;2:No&#39;, &#39;2:No&#39;, &#39;2:No&#39;, &#39;2:No&#39;, &#39;2:No&#39;, &#39;1:Yes&#39;, &#39;2:No&#39;, &#39;2:No&#39;, &#39;2:No&#39;, &#39;2:No&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;2:No&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;2:No&#39;, &#39;1:Yes&#39;, &#39;2:No&#39;, &#39;2:No&#39;, &#39;2:No&#39;, &#39;2:No&#39;, &#39;1:Yes&#39;, &#39;2:No&#39;, &#39;2:No&#39;, &#39;3:Unknown&#39;, &#39;2:No&#39;, &#39;2:No&#39;, &#39;2:No&#39;, &#39;2:No&#39;, &#39;3:Unknown&#39;, &#39;1:Yes&#39;, &#39;2:No&#39;, &#39;1:Yes&#39;, &#39;2:No&#39;, &#39;1:Yes&#39;, &#39;1:Yes&#39;, &#39;2:No&#39;],
        &#39;Proximity of spare part supplier&#39;: [0, 42, 180, 1000, 200, 45, 22, 90, 26, 80, 2, 5000, 60, 35, 800, 60, 1200, 10, 40, 5, 2, 200, 10, 200, 40, 138, 60, 48, 45, 45, 30, 56, 410, 0, 30, 180, 35, 5, 98, 3],
        &#39;declared ODF?&#39;: [&#39;No&#39;, &#39;No&#39;, &#39;No&#39;, &#39;Yes&#39;, &#39;No&#39;, &#39;No&#39;, &#39;Yes&#39;, &#39;No&#39;, &#39;Yes&#39;, &#39;No&#39;, &quot;Don&#39;t know&quot;, &#39;No&#39;, &#39;Yes&#39;, &#39;Yes&#39;, &#39;Yes&#39;, &#39;Yes&#39;, &#39;Yes&#39;, &quot;Don&#39;t know&quot;, &#39;No&#39;, &#39;Yes&#39;, &#39;No&#39;, &#39;No&#39;, &#39;No&#39;, &#39;No&#39;, &#39;No&#39;, &#39;No&#39;, &#39;No&#39;, &#39;No&#39;, &#39;No&#39;, &quot;Don&#39;t know&quot;, &#39;No&#39;, &#39;Yes&#39;, &quot;Don&#39;t know&quot;, &#39;No&#39;, &#39;No&#39;, &quot;Don&#39;t know&quot;, &#39;No&#39;, &#39;No&#39;, &#39;No&#39;, &#39;No&#39;]}
wp_clean = pd.DataFrame(data)
cols_x = wp_clean[[&#39;Region&#39;,&#39;District&#39;,&#39;Type of water point&#39;, &#39;Water point Functionality&#39;,&#39;WP_suitability&#39;,
&#39;Ownership&#39;,&#39;Maintenance in charge&#39;,&#39;WASH management committee?&#39;]]
# top 5 rows of columns to clean
Region   District             Type of water point        Water point Functionality WP_suitability             Ownership        Maintenance in charge WASH management committee?
0   East       Kono  11:Dam/pan (runoff harvesting)  1:Yes – Functional (and in use)          1:Yes           1:Community  1:WASH management committee                      1:Yes
1  South         Bo          9:Unprotected dug well  1:Yes – Functional (and in use)          1:Yes  8:Private Individual               3:Privat owner                       2:No
2  South    Pujehun         3:Tube well or borehole  1:Yes – Functional (and in use)          1:Yes           1:Community      2:Other community group                       2:No
3  North  Port Loko            2:Protected dug well  1:Yes – Functional (and in use)          1:Yes           1:Community      2:Other community group                       2:No
4  North  Koinadugu            2:Protected dug well               4:No - Broken down           2:No     6:Health Facility      2:Other community group                       2:No

Above is a dataframe of all the columns with the special character

I tried with this code, it works but I want a single function since this looks messy.

wp_clean[&#39;Type of water point&#39;] = wp_clean[&#39;Type of water point&#39;].str.split(&#39;:&#39;, n=1).str.get(-1)
wp_clean[&#39;Water point Functionality&#39;] = wp_clean[&#39;Water point Functionality&#39;].str.split(&#39;:&#39;, n=1).str.get(-1)
wp_clean[&#39;WP_suitability&#39;] = wp_clean[&#39;WP_suitability&#39;].str.split(&#39;:&#39;, n=1).str.get(-1)
wp_clean[&#39;Ownership&#39;] = wp_clean[&#39;Ownership&#39;].str.split(&#39;:&#39;, n=1).str.get(-1)
wp_clean[&#39;Maintenance in charge&#39;] = wp_clean[&#39;Maintenance in charge&#39;].str.split(&#39;:&#39;, n=1).str.get(-1)
wp_clean[&#39;WASH management committee?&#39;] = wp_clean[&#39;WASH management committee?&#39;].str.split(&#39;:&#39;, n=1).str.get(-1)

答案1

得分: 3

使用.applylambda函数来将一个函数应用于多个列。

# 要清理的列
cols = ['Region', 'District', 'Type of water point', 'Water point Functionality', 'WP_suitability', 'Ownership', 'Maintenance in charge', 'WASH management committee?']

# 清理列并将值分配回数据框
wp_clean[cols] = wp_clean[cols].apply(lambda col: col.str.split(':', n=1).str.get(-1))

结果

   Region        District           Chiefdom                                Section                                 wp Name  Latitude  Longitude  Elevation                               Type of water point          Water point Functionality      Water Sustainability WP_suitability  Year of construction           Ownership      Maintenance in charge WASH management committee?  Proximity of spare part supplier declared ODF?
0    East            Kono           Nimiyama                                Sewafeh                                  sokoya  8.583320 -11.221759      328.0                       Dam/pan (runoff harvesting)      Yes – Functional (and in use)                  Seasonal            Yes                  1990           Community  WASH management committee                        Yes                                 0            No
1   South              Bo            Bo Town  Second Bongay street coronation field                    More Rogers compound  7.954981 -11.738579      150.3                              Unprotected dug well      Yes – Functional (and in use)                  Seasonal            Yes                  2003  Private Individual               Privat owner                         No                                42            No
2   South         Pujehun              Barri                         Sembehun Barri                   Sembehun drilled well  7.400370 -11.385908       80.0                             Tube well or borehole      Yes – Functional (and in use)                  Seasonal            Yes                  2016           Community      Other community group                         No                               180            No
3   North       Port Loko         Lokomasama                                   Raka                         community  well  8.707599 -12.996102       58.0                                Protected dug well      Yes – Functional (and in use)                  Seasonal            Yes                  1984           Community      Other community group                         No                              1000           Yes
4   North       Koinadugu              Nieni                           Bandakarifya  Bandakarifya Health facility hand pump  9.269263 -11.193113      505.0                                Protected dug well                   No - Broken down                  Seasonal             No                  2010     Health Facility      Other community group                         No                               200            No
5   South              Bo            Bo Town                    J 11 Nikibu Section                              Mr  Bangle  7.959563 -11.749480      132.3                              Unprotected dug well      Yes – Functional (and in use)                  Seasonal            Yes                  2006  Private Individual               Privat owner                         No                                45            No
6    East            Kono             Gbense                           Hill station                           Kamara family  8.644247 -10.952336      421.0                                Protected dug well      Yes – Functional (and in use)              Always water            Yes                  2011  Private Individual               Privat owner                         No                                22           Yes
7   North       Koinadugu              Diang                             Kondaibaia                           Hand dug well  9.380305 -11.565382      394.0                                Protected dug well                   No - Broken down              Always water             No                  2013           Community  WASH management committee                        Yes                                90            No
8   North       Koinadugu  Dembelia-Sinkunia                         Gbindi Central                        Samba Water well  9.911492 -11.447203      388.0                                Protected dug well      Yes – Functional (and in use)              Always water            Yes                  1998  Private Individual               Privat owner                         No                                26           Yes
9   West    Western Urban           West III                               Aberdeen                          9 Kimbima road  8.498674 -13.287764       51.0               Piped water into dwelling/plot/yard      Yes – Functional (and in use)              Always water            Yes                  2011  Private Individual               Privat owner                         No                                80            No
10  West   Western Rural      Waterloo Rural                            Bass Street                              indian mac  8.322114 -13.075412       54.8                                Protected dug well                  Yes - But damaged                  Seasonal            Yes                  2013  Private Individual               Privat owner                         No                                 2    Don&#39;t know
11  South         Pujehun            Makpele                                  Dombu                         Guboi Community  7.315254 -11.263177      172.0                                Protected dug well      Yes – Functional (and in use)                  Seasonal            Yes                  2005           Community      Other community group                         No                              5000            No
12  North         Bombali        Sella Limba                       Kathantha Yimbor         Wcsl pri School well kathantha.  9.547058 -12.170908      129.3                                Protected dug well  Yes – Functional (but not in use)                  Seasonal            Yes                  2013              School                        NGO                        Yes                                60           Yes
13   East            Kono           Nimikoro                                  jiama                       public stand pipe  8.549673 -11.087326      413.0  Public tap/standpipe (stand-alone or water kiosk      Yes – Functional (and in use)              Always water            Yes                  1986           Community  WASH management committee                        Yes                                35           Yes
14  South         Pujehun                NaN                               Mandaima                                Mandaima  7.442514 -11.746506       94.0                                  Protected spring                   No - Broken down  Dry always / Never water             No                  2000              School                        NGO                        Yes                               800           Yes
15  South         Pujehun    Pejeh(Futa peje                               Njagbema                      Njagbema Community  7.506509 -11.524602      113.0                                Protected dug well                   No - Broken down              Always water            Yes                  2008           Community      Other community group                         No                                60           Yes
16  North       Port Loko         Lokomasama                                 mapang                             school well  8.778933 -13.092156       31.5                                Protected dug well                   No - Broken down                  Seasonal            Yes                  2013              School  WASH management committee                        Yes                              1200           Yes
17   East          Kenema     Kandu Leppiama                         Baoma oil mill                                     CHC  8.042020 -11.328288      141.6                                Protected dug well      Yes – Functional (and in use)                  Seasonal            Yes                  2015     Health Facility            Health Facility                        Yes                                10    Don&#39;t know
18  South              Bo            Bo Town           Gb
<details>
<summary>英文:</summary>
Use [`.apply`][1] with a `lambda` function to apply a function to multiple columns.
```python
# columns to clean
cols = [&#39;Region&#39;, &#39;District&#39;, &#39;Type of water point&#39;, &#39;Water point Functionality&#39;, &#39;WP_suitability&#39;, &#39;Ownership&#39;, &#39;Maintenance in charge&#39;, &#39;WASH management committee?&#39;]
# clean the columns and assign the values back to the dataframe
wp_clean[cols] = wp_clean[cols].apply(lambda col: col.str.split(&#39;:&#39;, n=1).str.get(-1))

Result

   Region        District           Chiefdom                                Section                                 wp Name  Latitude  Longitude  Elevation                               Type of water point          Water point Functionality      Water Sustainability WP_suitability  Year of construction           Ownership      Maintenance in charge WASH management committee?  Proximity of spare part supplier declared ODF?
0    East            Kono           Nimiyama                                Sewafeh                                  sokoya  8.583320 -11.221759      328.0                       Dam/pan (runoff harvesting)      Yes – Functional (and in use)                  Seasonal            Yes                  1990           Community  WASH management committee                        Yes                                 0            No
1   South              Bo            Bo Town  Second Bongay street coronation field                    More Rogers compound  7.954981 -11.738579      150.3                              Unprotected dug well      Yes – Functional (and in use)                  Seasonal            Yes                  2003  Private Individual               Privat owner                         No                                42            No
2   South         Pujehun              Barri                         Sembehun Barri                   Sembehun drilled well  7.400370 -11.385908       80.0                             Tube well or borehole      Yes – Functional (and in use)                  Seasonal            Yes                  2016           Community      Other community group                         No                               180            No
3   North       Port Loko         Lokomasama                                   Raka                         community  well  8.707599 -12.996102       58.0                                Protected dug well      Yes – Functional (and in use)                  Seasonal            Yes                  1984           Community      Other community group                         No                              1000           Yes
4   North       Koinadugu              Nieni                           Bandakarifya  Bandakarifya Health facility hand pump  9.269263 -11.193113      505.0                                Protected dug well                   No - Broken down                  Seasonal             No                  2010     Health Facility      Other community group                         No                               200            No
5   South              Bo            Bo Town                    J 11 Nikibu Section                              Mr  Bangle  7.959563 -11.749480      132.3                              Unprotected dug well      Yes – Functional (and in use)                  Seasonal            Yes                  2006  Private Individual               Privat owner                         No                                45            No
6    East            Kono             Gbense                           Hill station                           Kamara family  8.644247 -10.952336      421.0                                Protected dug well      Yes – Functional (and in use)              Always water            Yes                  2011  Private Individual               Privat owner                         No                                22           Yes
7   North       Koinadugu              Diang                             Kondaibaia                           Hand dug well  9.380305 -11.565382      394.0                                Protected dug well                   No - Broken down              Always water             No                  2013           Community  WASH management committee                        Yes                                90            No
8   North       Koinadugu  Dembelia-Sinkunia                         Gbindi Central                        Samba Water well  9.911492 -11.447203      388.0                                Protected dug well      Yes – Functional (and in use)              Always water            Yes                  1998  Private Individual               Privat owner                         No                                26           Yes
9   West    Western Urban           West III                               Aberdeen                          9 Kimbima road  8.498674 -13.287764       51.0               Piped water into dwelling/plot/yard      Yes – Functional (and in use)              Always water            Yes                  2011  Private Individual               Privat owner                         No                                80            No
10  West   Western Rural      Waterloo Rural                            Bass Street                              indian mac  8.322114 -13.075412       54.8                                Protected dug well                  Yes - But damaged                  Seasonal            Yes                  2013  Private Individual               Privat owner                         No                                 2    Don&#39;t know
11  South         Pujehun            Makpele                                  Dombu                         Guboi Community  7.315254 -11.263177      172.0                                Protected dug well      Yes – Functional (and in use)                  Seasonal            Yes                  2005           Community      Other community group                         No                              5000            No
12  North         Bombali        Sella Limba                       Kathantha Yimbor         Wcsl pri School well kathantha.  9.547058 -12.170908      129.3                                Protected dug well  Yes – Functional (but not in use)                  Seasonal            Yes                  2013              School                        NGO                        Yes                                60           Yes
13   East            Kono           Nimikoro                                  jiama                       public stand pipe  8.549673 -11.087326      413.0  Public tap/standpipe (stand-alone or water kiosk      Yes – Functional (and in use)              Always water            Yes                  1986           Community  WASH management committee                        Yes                                35           Yes
14  South         Pujehun                NaN                               Mandaima                                Mandaima  7.442514 -11.746506       94.0                                  Protected spring                   No - Broken down  Dry always / Never water             No                  2000              School                        NGO                        Yes                               800           Yes
15  South         Pujehun    Pejeh(Futa peje                               Njagbema                      Njagbema Community  7.506509 -11.524602      113.0                                Protected dug well                   No - Broken down              Always water            Yes                  2008           Community      Other community group                         No                                60           Yes
16  North       Port Loko         Lokomasama                                 mapang                             school well  8.778933 -13.092156       31.5                                Protected dug well                   No - Broken down                  Seasonal            Yes                  2013              School  WASH management committee                        Yes                              1200           Yes
17   East          Kenema     Kandu Leppiama                         Baoma oil mill                                     CHC  8.042020 -11.328288      141.6                                Protected dug well      Yes – Functional (and in use)                  Seasonal            Yes                  2015     Health Facility            Health Facility                        Yes                                10    Don&#39;t know
18  South              Bo            Bo Town           Gbonda Road Coronation Field                  More Sarah water point  7.956939 -11.740514      193.3                              Unprotected dug well      Yes – Functional (and in use)                  Seasonal            Yes                  2008  Private Individual               Privat owner                         No                                40            No
19  North       Koinadugu             Sengbe                             Dankawalie                    mamayeria water well  9.631007 -11.329491      457.0                                Protected dug well      Yes – Functional (and in use)                  Seasonal            Yes                  1985           Community  WASH management committee                        Yes                                 5           Yes
20   East            Kono       Gbane Kandor                          Fanda Viĺlage                                  Strime  8.640975 -10.599015      264.0                 Surface water (lake/river/stream)          No - Under rehabilitation                  Seasonal            Yes                  1970           Community              No management                         No                                 2            No
21  North       Koinadugu              Nieni                                 Kumala                    Kumala community tap  9.064445 -11.402490      441.0  Public tap/standpipe (stand-alone or water kiosk                   No - Broken down              Always water             No                  1991           Community      Other community group                         No                               200            No
22  West    Western Urban           West III                                 Lumley                      16 E Freetown Road  8.455972 -13.271662       58.0               Piped water into dwelling/plot/yard      Yes – Functional (and in use)              Always water            Yes                  2013  Private Individual               Privat owner                         No                                10            No
23  North       Koinadugu              Nieni                                 Kumala                    Kumala community tap  9.063575 -11.401967      428.0  Public tap/standpipe (stand-alone or water kiosk                   No - Broken down              Always water             No                  1991           Community      Other community group                         No                               200            No
24   East        Kailahun              Mandu                                  Mobai                                    Tobu  7.992935 -10.752254      223.6  Public tap/standpipe (stand-alone or water kiosk      Yes – Functional (and in use)                  Seasonal            Yes                  1982           Community      Other community group                        Yes                                40            No
25  South              Bo            Valunia                               Baomahun                Baomahun community water  8.414375 -11.669410      147.5                              Unprotected dug well      Yes – Functional (and in use)              Always water            Yes                  2010  Private Individual               Privat owner                         No                               138            No
26  West    Western Urban           East III                         Jalloh terrace      Amidu Bah water point 12 city road  8.464235 -13.181635      120.3                                Protected dug well      Yes – Functional (and in use)              Always water            Yes                  2000  Private Individual               Privat owner                         No                                60            No
27  North         Bombali        Sella Limba                            Kabba Ferry                  Kabba ferry com well 3  9.586626 -12.217859       42.7                                Protected dug well                   No - Broken down  Dry always / Never water         Uknown                  2005           Community                        NGO                    Unknown                                48            No
28  South              Bo            Bo Town             Tenp.nant Street Niagboima                   SALWACO   Water Point  7.954811 -11.749736      124.2                                Protected dug well      Yes – Functional (and in use)                  Seasonal            Yes                  2009  Private Individual               Privat owner                         No                                45            No
29  West   Western Rural      Mountain Rural                                 Regent                7 Gloucester Road Regent  8.435817 -13.218309      409.0               Piped water into dwelling/plot/yard      Yes – Functional (and in use)                  Seasonal            Yes                  2010  Private Individual               Privat owner                         No                                45    Don&#39;t know
30  West    Western Urban             West I                   Siaka Steven Stadium                         Stadium Hostels  8.480454 -13.249449       31.0  Public tap/standpipe (stand-alone or water kiosk      Yes – Functional (and in use)              Always water            Yes                  1979                GUMA                       GUMA                         No                                30            No
31  South         Pujehun              Kpaka                                   Soso                            Sowa compand  7.295935 -11.655026       57.0                                Protected dug well  Yes – Functional (but not in use)                  Seasonal            Yes                  2014           Community  WASH management committee                         No                                56           Yes
32  South         Pujehun            Makpele                            Jorporwahun                                Kallonla  7.340454 -11.334209      127.0                                Protected dug well      Yes – Functional (and in use)                  Seasonal            Yes                  2014           Community                        NGO                    Unknown                               410    Don&#39;t know
33  South         Moyamba              Ribbi                         foryah village                   foryah community well  8.291900 -12.779628      -74.9                                Protected dug well                  Yes - But damaged              Always water            Yes                  1985           Community      Other community group                        Yes                                 0            No
34   East            Kono           Nimikoro                                Yendema                           Hand dug well  8.571986 -11.093100      432.0                             Tube well or borehole      Yes – Functional (and in use)              Always water            Yes                  2009           Community      Other community group                         No                                30            No
35   East          Kenema              Gaura                                 Perrie                           Lanssana Jumu  7.660193 -11.079632      103.8  Public tap/standpipe (stand-alone or water kiosk                  Yes - But damaged                  Seasonal            Yes                  1988           Community  WASH management committee                        Yes                               180    Don&#39;t know
36  South              Bo            Bo Town                           Forth Street                           Forth  Street  7.955801 -11.748337      152.5                              Unprotected dug well      Yes – Functional (and in use)                  Seasonal            Yes                  2006  Private Individual               Privat owner                         No                                35            No
37  West    Western Urban            West II                          Assesion Twon                               Gray Bush  8.481693 -13.249361       87.3  Public tap/standpipe (stand-alone or water kiosk      Yes – Functional (and in use)              Always water            Yes                  2015           Community  WASH management committee                        Yes                                 5            No
38   East        Kailahun              Mandu                                  Yoyah                          community pump  7.930078 -10.766633      253.0                                Protected dug well      Yes – Functional (and in use)                  Seasonal            Yes                  2004           Community  WASH management committee                        Yes                                98            No
39  South              Bo            Bo Town                               Cee Line                                 Private  7.952044 -11.733190      231.3                              Unprotected dug well      Yes – Functional (and in use)                  Seasonal            Yes                  2013  Private Individual               Privat owner                         No                                 3            No

huangapple
  • 本文由 发表于 2023年5月17日 09:46:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定