利用pandas进行数据预处理

2020-04-30 10:50:00 by scott

数据处理

概述

预处理数据是使用数据的必要步骤。

这里使用pandas进行处理,效率虽然不是最优的,但方便性还是显而易见的。

场景:

从网页上(通过requests抓取一个json包)下载的一堆数据,是个字典列表。现在要把它转换后存储到自己的MySQL库里。需要处理为:转换字段名称修改字段类型去除字段前后的空字符、转换字符串为float

数据结构:

[
    {"时间": "2020-04-30", "广告展示数": 1240, "点击量": 65, "预计收入": 21,
     "广告位ID": "2050238408812559", "广告位": "应用A-安卓-01", "千次展示收益": 1.2},
   {"时间": "2020-04-30", "广告展示数": 1540, "点击量": 85, "预计收入": 31,
     "广告位ID": "2050238408812000", "广告位": "应用A-安卓-02", "千次展示收益": 1.3}
]

 

处理方法

COLUMON = {"时间": "date_time", "广告位": "union_position_name", "广告位ID": "union_position_id",
              "广告展示数": "display_pv",
              "点击量": "click_pv", "预计收入": "cost", "千次展示收益": "union_ecpm"}
df = pd.DataFrame(data)
# 修改字段名,设置inplace=True则会在原dataframe中替换
df.rename(columns=COLUMON, inplace=True)
# 为了防止出现赋值警告,这里复制出来一份
df = df[COLUMON.values()].copy()
# 把2020/04/30转换为2020-04-30
df['date_time'] = df['date_time'].map(lambda x: x.replace("/", "-"))
# 把字符串类型的数字转为浮点型,同时处理数字中的逗号
for col in ['display_pv', 'click_pv', 'cost', 'union_ecpm']:
    if df.dtypes[col] == np.dtype(np.object):
        df[col] = df[col].map(lambda x: float((x.replace(",", "") if isinstance(x, str) else x)))
# 去除字符串前后的空字符
df["union_position_name"] = df["union_position_name"].map(lambda x:x.strip())
# 把字段转换为字符串
df['union_position_id'] = df['union_position_id'].astype(str)

 

总结

虽然这个方法速度不一定是最快,但操作起来肯定是最方便的。

 


Comments(131) Add Your Comment

Charlesnag
“I haven’t seen you in these parts,” the barkeep said, sidling over and above to where I sat. “Name’s Bao.” He stated it exuberantly, as if solemn word of honour of his exploits were shared by settlers around multitudinous a verve in Aeternum. He waved to a unanimated butt beside us, and I returned his gesticulate with a nod. He filled a field-glasses and slid it to me across the stained red wood of the court prior to continuing. “As a betting man, I’d be assenting to wager a adequate speck of silver you’re in Ebonscale Reach in search more than the wet one's whistle and sights,” he said, eyes glancing from the sword sheathed on my with it to the salaam slung across my back. http://images.google.co.ke/url?q=https://renewworld.ru/otlichie-new-world-versii-deluxe-i-standart-edition/
Defhui
lipitor 10mg oral <a href="https://lipiws.top/">lipitor canada</a> buy lipitor 20mg pill
Ecpwjr
ciprofloxacin 1000mg generic - <a href="https://metroagyl.top/">ciprofloxacin cheap</a> buy augmentin 375mg sale
Iwmeih
purchase ciprofloxacin pill - <a href="https://cipropro.top/keflexcapsule/">cephalexin 125mg oral</a> buy augmentin 1000mg generic
Mwtryg
metronidazole 200mg price - <a href="https://metroagyl.top/clindamycin/">purchase cleocin online</a> order zithromax 250mg pill
Uvlfue
buy ciprofloxacin 500mg without prescription - <a href="https:/septrim.top/">order generic ciprofloxacin</a> erythromycin 250mg brand
Wqojsj
valtrex 1000mg us - <a href="https://gnantiviralp.com/starlix/">buy nateglinide 120 mg pill</a> buy zovirax 400mg without prescription
Ozjnbb
ivermectin 12mg without a doctor prescription - <a href="https://keflexin.top/cefixime/">order cefixime 200mg generic</a> order tetracycline pill
Ptgsbg
metronidazole 400mg pills - <a href="https://metroagyl.top/keflexcapsule/">buy amoxicillin medication</a> azithromycin 250mg generic
Rtfvmk
ampicillin oral <a href="https://ampiacil.top/penicillinused/">buy penicillin without a prescription</a> buy amoxicillin without a prescription
Nvjsna
furosemide online - <a href="https://antipathogenc.com/minipressm/">minipress 2mg price</a> buy capoten 25 mg generic
Qfhxub
buy generic glycomet for sale - <a href="https://gnantiacidity.com/septra/">buy trimethoprim generic</a> buy lincocin online cheap
Lhvyec
retrovir brand - <a href="https://canadiangnp.com/allopurinol/">zyloprim pills</a> cheap zyloprim 100mg
Mobnag
oral clozapine - <a href="https://genonlinep.com/accupril/">order quinapril 10 mg</a> famotidine sale
Hcntgw
quetiapine price - <a href="https://gnkantdepres.com/eskalith/">purchase eskalith without prescription</a> eskalith tablet
Oshtlk
anafranil for sale - <a href="https://antdeponline.com/remeron/">remeron online buy</a> sinequan 75mg brand
Jwkohm
order hydroxyzine generic - <a href="https://antdepls.com/buspar/">buspar 5mg sale</a> buy amitriptyline cheap
Lkysdm
amoxicillin price - <a href="https://atbioxotc.com/cephalexin/">brand keflex 250mg</a> ciprofloxacin usa
Yptfjy
order amoxiclav online - <a href="https://atbioinfo.com/">purchase augmentin pills</a> purchase ciprofloxacin sale
Piktpy
cleocin 300mg generic - <a href="https://cadbiot.com/chloramphenicol/">buy cheap chloramphenicol</a> buy chloromycetin pills for sale
Aruyxe
azithromycin 250mg price - <a href="https://gncatbp.com/emetronidazole/">metronidazole cheap</a> ciprofloxacin 500mg without prescription
Olmmiy
ivermectin human - <a href="https://antibpl.com/aqdapsone/">buy aczone medication</a> order cefaclor 500mg sale
Qwvahr
albuterol uk - <a href="https://antxallergic.com/cvallegra/">purchase allegra online</a> theo-24 Cr canada
Rhorkh
order clarinex online cheap - <a href="https://rxtallerg.com/">order desloratadine pills</a> albuterol 2mg brand
Cmhhwv
buy cheap methylprednisolone - <a href="https://ntallegpl.com/loratadine/">order loratadine 10mg generic</a> order azelastine sprayer
Krqhtd
cost micronase - <a href="https://prodeprpl.com/dapagliflozin5/">order dapagliflozin 10mg sale</a> forxiga generic
Xelcej
order prandin 1mg - <a href="https://depressinfo.com/">buy prandin pills</a> order jardiance 10mg sale
Pzkoil
order glycomet online - <a href="https://arxdepress.com/">buy cheap generic glycomet</a> buy precose without a prescription
Bmaiyj
where can i buy lamisil - <a href="https://treatfungusx.com/adiflucanv/">cost forcan</a> grifulvin v usa
Mooxjc
purchase semaglutide generic - <a href="https://infodeppl.com/">rybelsus 14mg without prescription</a> buy DDAVP generic
Fvumkj
order ketoconazole - <a href="https://antifungusrp.com/">buy nizoral cheap</a> buy itraconazole 100mg online
Bgtgkx
famciclovir 500mg oral - <a href="https://amvinherpes.com/gnvalaciclovir/">buy valcivir medication</a> order valaciclovir 1000mg pill
Vtrbmy
digoxin over the counter - <a href="https://blpressureok.com/">order digoxin 250 mg for sale</a> lasix 40mg price
Lfskyz
buy hydrochlorothiazide 25mg pills - <a href="https://norvapril.com/amlodipine/">norvasc over the counter</a> purchase bisoprolol
Awwvvy
buy metoprolol generic - <a href="https://bloodpresspl.com/olmesartan/">order benicar online</a> adalat over the counter
Aqitjc
buy nitroglycerin medication - <a href="https://nitroproxl.com/">order nitroglycerin sale</a> order diovan pills
Ikcqyh
crestor pills decay - <a href="https://antcholesterol.com/pravastatin/">pravastatin buy breast</a> caduet online type
Fdsjif
zocor pile - <a href="https://canescholest.com/">simvastatin objection</a> lipitor slip
Tdabal
viagra professional fool - <a href="https://edsildps.com/superavana/">avana cottage</a> levitra oral jelly uncomfortable
Ymwkyt
priligy achieve - <a href="https://promedprili.com/sildigra/">sildigra bother</a> cialis with dapoxetine england
Jeekaa
cenforce nonsense - <a href="https://xcenforcem.com/levitraprofessional/">levitra professional start</a> brand viagra accept
Bcdcdn
brand cialis bond - <a href="https://probrandtad.com/penisole/">penisole meanwhile</a> penisole hunter
Jfkgne
cialis soft tabs forbid - <a href="https://supervalip.com/viagrasuperactive/">viagra super active online grip</a> viagra oral jelly dear
Exlkqg
brand cialis request - <a href="https://probrandtad.com/viagrasofttabs/">viagra soft tabs across</a> penisole twin
Lwbcmu
cialis soft tabs lighter - <a href="https://supervalip.com/tadarisepills/">tadarise grant</a> viagra oral jelly online doubtful
Yhrzmd
cenforce strict - <a href="https://xcenforcem.com/zenegrasildenafil/">zenegra online chest</a> brand viagra online personality
Qztkst
priligy six - <a href="https://promedprili.com/viagraplus/">viagra plus gown</a> cialis with dapoxetine stuff
Gghgyo
acne treatment noble - <a href="https://placnemedx.com/">acne medication impression</a> acne medication waist
Xssrji
inhalers for asthma swear - <a href="https://bsasthmaps.com/">inhalers for asthma wick</a> inhalers for asthma inner
Cartyo
treatment for uti prefer - <a href="https://amenahealthp.com/">uti treatment subject</a> uti antibiotics earnest
Bjuxkp
prostatitis pills modern - <a href="https://xprosttreat.com/">prostatitis pills green</a> prostatitis pills dangerous
Docglf
valtrex anyhow - <a href="https://gnantiviralp.com/">valtrex online size</a> valacyclovir online connection
Vzgxiq
claritin pills cough - <a href="https://clatadine.top/">claritin confusion</a> loratadine medication fly
Tzhrpw
claritin auditor - <a href="https://clatadine.top/">claritin pills ridge</a> loratadine courage
Izidxb
priligy dress - <a href="https://prilixgn.top/">dapoxetine yard</a> priligy right
Pezxfa
promethazine ball - <a href="https://prohnrg.com/">promethazine if</a> promethazine museum
Nuwdil
ascorbic acid beside - <a href="https://ascxacid.com/">ascorbic acid bigger</a> ascorbic acid ear
Mhgibc
clarithromycin vein - <a href="https://gastropls.com/gncytotec/">cytotec pills orange</a> cytotec nearby
Vedpda
fludrocortisone pills meal - <a href="https://gastroplusp.com/propantoprazole/">pantoprazole medical</a> prevacid wait
Gwefui
order bisacodyl 5mg pills - <a href="https://gastroinfop.com/">purchase bisacodyl generic</a> liv52 over the counter
Pkhxcl
aciphex online order - <a href="https://gastrointesl.com/">order rabeprazole 20mg online</a> motilium 10mg pills
Jvjrkp
order cotrimoxazole 960mg pill - <a href="https://ketiracet.com/">buy levetiracetam 1000mg</a> buy tobramycin cheap
Jgaaaw
purchase zovirax sale - <a href="https://cerazestrel.com/">buy desogestrel for sale</a> dydrogesterone 10mg for sale
Nnehcj
order forxiga 10 mg online cheap - <a href="https://forpaglif.com/">dapagliflozin without prescription</a> buy acarbose 25mg generic
Bjemuy
griseofulvin pill - <a href="https://fulviseoful.com/">where to buy fulvicin without a prescription</a> buy lopid generic
Smjhni
vasotec 10mg uk - <a href="https://vasolapril.com/">vasotec price</a> latanoprost order
Ieapwy
buy dramamine generic - <a href="https://bisacolax.com/">order dimenhydrinate 50mg generic</a> actonel 35 mg drug
Jzwast
etodolac 600mg uk - <a href="https://etodograph.com/">monograph 600 mg pills</a> cilostazol 100 mg tablet
Bhzcvx
brand feldene - <a href="https://rivastilons.com/">order rivastigmine generic</a> order rivastigmine online
Nqdksf
buy nootropil no prescription - <a href="https://nootquin.com/sinemet/">order sinemet online</a> order generic sinemet
Cdnkvn
hydroxyurea where to buy - <a href="https://hydroydrinfo.com/pentoxifylline/">brand pentoxifylline</a> order methocarbamol 500mg sale
Jvrnrf
divalproex over the counter - <a href="https://adepamox.com/acetazolamide/">order diamox 250mg sale</a> buy topamax
Gczkcd
buy disopyramide phosphate tablets - <a href="https://anorpica.com/pregabal/">order pregabalin without prescription</a> buy generic chlorpromazine online
Djclub
cheap cytoxan without prescription - <a href="https://cycloxalp.com/dimenhydrinate/">how to buy dramamine</a> trimetazidine online order
Zlfxnt
buy aldactone generic - <a href="https://aldantinep.com/dosulepin/">prothiaden usa</a> order naltrexone sale
Xkhbho
buy generic cyclobenzaprine online - <a href="https://abflequine.com/primaquine/">primaquine tablets</a> buy enalapril 10mg sale
Xnewwz
generic zofran 4mg - <a href="https://azofarininfo.com/selegiline/">buy selegiline for sale</a> requip 1mg canada
Ydwwua
buy ascorbic acid 500 mg for sale - <a href="https://mdacidinfo.com/bromhexine/">buy bromhexine pills</a> purchase prochlorperazine generic
Gholvb
buy cheap durex gel - <a href="https://xalaplinfo.com/latanoprosteyedrops/">cheap latanoprost</a> xalatan drug
Veynpx
purchase minoxidil for sale - <a href="https://hairlossmedinfo.com/finasteridefinp/">purchase finpecia generic</a> buy propecia 1mg online
Nodpzo
order leflunomide sale - <a href="https://infohealthybones.com/risedronate/">buy cheap actonel</a> buy generic cartidin for sale
Wmzbvf
order tenormin 100mg generic - <a href="https://heartmedinfox.com/sotalol/">sotalol ca</a> carvedilol online order
Pwrnlc
verapamil 120mg generic - <a href="https://infoheartdisea.com/">order verapamil 240mg online cheap</a> order tenoretic generic
Libcwz
buy atorvastatin for sale - <a href="https://infoxheartmed.com/">atorlip without prescription</a> nebivolol 20mg sale
Dwuawq
buy gasex online cheap - <a href="https://herbalinfomez.com/ashwagandha/">ashwagandha order</a> order diabecon online cheap
Scavrw
purchase lasuna online cheap - <a href="https://infoherbalmz.com/diarex/">where to buy diarex without a prescription</a> himcolin cheap
Jtczfy
purchase norfloxacin pill - <a href="https://gmenshth.com/flutamide/">buy flutamide no prescription</a> purchase confido generic
Snmlex
buy speman generic - <a href="https://spmensht.com/himplasia/">order himplasia online cheap</a> finasteride over the counter
Aftmxk
finasteride sale - <a href="https://finmenura.com/">buy finasteride no prescription</a> alfuzosin 10mg us
Utwywi
buy terazosin no prescription - <a href="https://hymenmax.com/tamsulosin/">order tamsulosin 0.4mg without prescription</a> priligy 90mg canada
Igqakv
oxcarbazepine over the counter - <a href="https://trileoxine.com/pirfenidone/">purchase pirfenex generic</a> levothyroxine order
Ynvauc
lactulose tubes - <a href="https://duphalinfo.com/mentat/">brahmi order online</a> betahistine 16mg generic
Bfzgpb
buy generic cyclosporine over the counter - <a href="https://asimusxate.com/">buy imusporin generic</a> gloperba for sale
Qjchhn
order calcort pills - <a href="https://lazacort.com/brimonidine/">brimonidine cost</a> buy generic alphagan for sale
Fhjmjg
besifloxacin eye drops - <a href="https://besifxcist.com/carbocysteine/">buy carbocisteine generic</a> cheap sildamax for sale
Kmloyf
order neurontin pill - <a href="https://aneutrin.com/ibuprofen/">order ibuprofen 400mg generic</a> azulfidine 500mg us
Cuwffp
buy probalan - <a href="https://bendoltol.com/monograph/">order etodolac generic</a> where can i buy carbamazepine
Ouoerc
buy mebeverine 135 mg for sale - <a href="https://coloxia.com/etoricoxib/">order etoricoxib 120mg without prescription</a> cheap pletal
Svricy
buy celecoxib 200mg online - <a href="https://celespas.com/indomethacin/">buy generic indocin</a> indomethacin 50mg sale
Rskzlb
voltaren cheap - <a href="https://dicloltarin.com/">purchase voltaren for sale</a> purchase aspirin online cheap
Pzikyo
rumalaya canada - <a href="https://rumaxtol.com/">purchase rumalaya for sale</a> endep 50mg canada
Ekinwq
mestinon 60mg brand - <a href="https://mestonsx.com/asumatriptan/">imitrex price</a> azathioprine 25mg sale
Ngeeyn
cheap diclofenac without prescription - <a href="https://vovetosa.com/tisosorbide/">buy imdur cheap</a> buy nimotop for sale
Uyrxkn
order ozobax generic - <a href="https://baclion.com/nspiroxicam/">buy generic feldene</a> buy piroxicam 20mg sale
Mxhapj
order cyproheptadine 4 mg generic - <a href="https://periheptadn.com/">cyproheptadine 4 mg for sale</a> buy tizanidine online
Kxvugk
order artane without prescription - <a href="https://voltapll.com.com/">order generic artane</a> buy diclofenac gel
Gebcqm
buy omnicef 300mg online cheap - <a href="https://omnixcin.com/aminocycline/"></a> cleocin ca
Jzqojo
order absorica online - <a href="https://aisotane.com/asdeltasone/">deltasone 20mg cheap</a> buy deltasone 5mg without prescription
Nmkhgp
acticin for sale - <a href="https://actizacs.com/asbenzac/">buy benzac cream for sale</a> purchase retin generic
Tlxmip
oral metronidazole 200mg - <a href="https://cenforcevs.com/">buy cenforce 100mg generic</a> cenforce pills
Zlughv
betamethasone 20gm canada - <a href="https://betnoson.com/awdifferin/">where to buy adapalene without a prescription</a> purchase monobenzone online
Sffpam
buy clavulanate generic - <a href="https://alevonted.com/">buy levothyroxine paypal</a> brand levothroid
Syolsr
buy generic cleocin - <a href="https://clinycinpl.com/">order cleocin 150mg</a> buy indocin no prescription
Uiktff
cost cozaar - <a href="https://cozartan.com/">hyzaar uk</a> order keflex 500mg sale
Locmhc
crotamiton ca - <a href="https://aeuracream.com/">order eurax generic</a> aczone tablet
Gfioqe
bupropion 150 mg drug - <a href="https://bupropsl.com/orlistat/">xenical ca</a> shuddha guggulu tablets
Klaxqk
cost provigil 100mg - <a href="https://sleepagil.com/">modafinil 200mg pills</a> meloset online order
Otixql
buy prometrium tablets - <a href="https://apromid.com/">buy progesterone 200mg pill</a> clomiphene online order
Xbwezk
xeloda 500 mg for sale - <a href="https://xelocap.com/">order generic xeloda</a> buy danazol generic
Vrorcu
buy aygestin generic - <a href="https://norethgep.com/lumigan/">lumigan for sale online</a> yasmin price
Cgqkti
oral fosamax - <a href="https://pilaxmax.com/tamoxifen/">nolvadex order online</a> medroxyprogesterone buy online
Sabbam
cabergoline 0.25mg for sale - <a href="https://adostilin.com/cabgolin/">order cabgolin without prescription</a> cheap alesse online
Combbo
estrace drug - <a href="https://festrolp.com/anastrozole/">arimidex 1mg sale</a> purchase anastrozole
Rbzdkh
バイアグラ 値段 - <a href="https://jpedpharm.com/">バイアグラ通販で買えますか</a> シアリス おすすめ
Mcnrfs
гѓ—гѓ¬гѓ‰гѓ‹гѓійЂљиІ©гЃЉгЃ™гЃ™г‚Ѓ - <a href="https://jpaonlinep.com/jamoxicillin/">г‚ўгѓўг‚­г‚·гѓ« - 500mg</a> г‚ёг‚№гѓ­гѓћгѓѓг‚Ї - 500mg
Sgbkyg
eriacta gentlemen - <a href="https://eriagra.com/">eriacta finger</a> forzest justice
Ganucu
buy generic indinavir over the counter - <a href="https://confindin.com/fincar/">buy finasteride sale</a> buy emulgel for sale
Lnoewl
valif online mistake - <a href="https://avaltiva.com/awsustiva/">buy sustiva generic</a> sinemet 10mg cost
Qyjafi
where can i buy modafinil - <a href="https://provicef.com/cefadroxil/">order cefadroxil 500mg online cheap</a> buy cheap generic epivir
Fscaoc
ivermectin 3mg oral - <a href="https://ivercand.com/carbamazepine/">tegretol 200mg without prescription</a> how to get tegretol without a prescription
Taaiur
buy cheap promethazine - <a href="https://prometacin.com/jilincomycin/">buy lincocin 500mg pills</a> buy lincomycin for sale