① python:定義函數,輸入一個m維數組X和一個整數n,輸出一個n*m的矩陣M,其中 M[i] [j]=
#Python3.6
importnumpyasnp
whileTrue:
x=input("輸入一個m維數組,元素以空格分開:")
try:
#splitinput
x=x.split()
s=[]
#iterate:str-->int
foriinx:
s.append(int(i))
x=s
break
except:
print("輸入有錯,請重新輸入。")
whileTrue:
n=input("整數n:")
try:
n=int(n)
break
except:
print("輸入有錯,請重新輸入。")
#mapobject-->listobject
lst=[iforiinx]
#listobject-->numpy.arrayobject
mtrx=np.array(lst)
#the1stlineofmatrixasatemplate
mtrx_1=np.(mtrx)
k=len(mtrx)
#reshape(k,)to(1,k)forconcatenate
mtrx=np.reshape(mtrx,(1,k))
#calculateleftlines
foriinrange(1,n):
tmp=np.power(mtrx_1,i+1)
tmp=np.reshape(tmp,(1,k))
mtrx=np.concatenate((mtrx,tmp),axis=0)
print(mtrx)
② python(pandas模塊)
1.什麼是pandas? numpy模塊和pandas模塊都是用於處理數據的模塊。 numpy主要用於針對數組進行統計計算,處理數字數據比較方便。 pandas除了可以處理數字數據,還可...
③ Python課程內容都學習什麼啊
賀聖軍Python輕松入門到項目實戰(經典完整版)(超清視頻)網路網盤
鏈接: https://pan..com/s/1C9k1o65FuQKNe68L3xEx3w
若資源有問題歡迎追問~
④ 鎳侫ccess琛ㄧ殑榪涙潵涓涓嬶紝澶氳〃榪炴帴鑱氬悎鍑芥暟錛岃阿璋
瑕佸疄鐜頒綘鐨勮佹眰錛屽彲浠ヤ嬌鐢⊿QL鐨勮繛鎺ワ紙JOIN錛夊拰鑱氬悎鍑芥暟錛圓ggregate Functions錛夈傝繖閲屼嬌鐢ㄧ殑鏄疌OUNT鍑芥暟鍜孏ROUP BY瀛愬彞銆備互涓嬫槸涓涓鍙鑳界殑SQL鏌ヨ錛
sql澶嶅埗浠g爜
SELECT S.name AS涓氬姟鍛,
P1.name AS鍟嗗搧鍚1,
P3.name AS鍟嗗搧鍚3,
P8.name AS鍟嗗搧鍚8,
SUM(O.money) AS閲戦
FROM sales S
LEFT JOIN orders O ON S.ID = O.S.ID
LEFT JOIN proct P1 ON P1.ID = O.P.ID AND P1.name = '鍟嗗搧鍚1'
LEFT JOIN proct P3 ON P3.ID = O.P.ID AND P3.name = '鍟嗗搧鍚3'
LEFT JOIN proct P8 ON P8.ID = O.P.ID AND P8.name = '鍟嗗搧鍚8'
WHERE O.date = '鎸囧畾鏃ユ湡'
GROUP BY S.name, P1.name, P3.name, P8.name;
鍦ㄨ繖涓猄QL鏌ヨ涓錛屾垜浠棣栧厛浠巗ales琛ㄥ拰orders琛ㄤ腑榪炴帴浜嗘暟鎹錛岀劧鍚庝粠proct琛ㄤ腑榪炴帴浜嗘暟鎹錛屾潯浠舵槸proct琛ㄤ腑鐨勫晢鍝佸悕瑕佸拰鎸囧畾鐨勫晢鍝佸悕涓鑷淬傜劧鍚庢垜浠瀵規墍鏈夎繛鎺ョ殑鏁版嵁榪涜屼簡GROUP BY鎿嶄綔錛屾妸鐩稿悓涓氬姟鍛樺拰鐩稿悓鍟嗗搧鍚嶇殑鏁版嵁鏀懼湪涓璧楓傛渶鍚庢垜浠瀵規瘡涓緇勭殑閲戦濊繘琛屼簡奼傚拰銆
璇鋒敞鎰忥紝榪欎釜鏌ヨ㈠亣璁炬瘡涓鍟嗗搧鍚嶅湪鍟嗗搧琛ㄤ腑閮芥槸鍞涓鐨勶紝鑰屼笖姣忎釜璁㈠崟鍙瀵瑰簲涓涓鍟嗗搧銆傚傛灉榪欎簺鍋囪句笉鎴愮珛錛屼綘鍙鑳介渶瑕佸規煡璇㈣繘琛屼慨鏀廣
鍙﹀栵紝浣犻渶瑕佸皢'鎸囧畾鏃ユ湡'鏇挎崲涓哄疄闄呯殑鏃ユ湡銆
⑤ python分析奧巴馬資金來源
奧巴馬的競選資金是一點點從選民那裡募集來的。如獲黨內提名,可得政府拔款,但也沒多飢碼少。美國大選不僅禁外國人捐款,而且禁止公司機構捐款,而只允許個人捐款。不僅如此,還為個人捐款限制了上限,防止富人捐過多的款而影響未來的公旦姿平執政。
不僅富人自己不能多捐,如果某個老闆呼籲自己的員工給某人捐錢或投票支持他爛遲哪,都是犯法的。因此,想要籌到幾千萬競爭資金,唯一的辦法是爭取更多選民支持,一點點募集。所以,中國、公司、大筆捐款,這三條都是犯法的。
我記得以前已經有華人鬧過這種丑聞了。美國的選舉法就是要嚴防少數人企圖用幾個臭錢影響美國的政治。所以我們作為外國人就更別去自討沒趣了。
導入包
In [1]:
import numpy as npimport pandas as pdfrom pandas import Series,DataFrame
方便大家操作,將月份和參選人以及所在政黨進行定義
In [2]:
months = {'JAN' : 1, 'FEB' : 2, 'MAR' : 3, 'APR' : 4, 'MAY' : 5, 'JUN' : 6, 'JUL' : 7, 'AUG' : 8, 'SEP' : 9, 'OCT': 10, 'NOV': 11, 'DEC' : 12}of_interest = ['Obama, Barack', 'Romney, Mitt', 'Santorum, Rick', 'Paul, Ron', 'Gingrich, Newt']parties = { 'Bachmann, Michelle': 'Republican', 'Romney, Mitt': 'Republican', 'Obama, Barack': 'Democrat', "Roemer, Charles E. 'Buddy' III": 'Reform', 'Pawlenty, Timothy': 'Republican', 'Johnson, Gary Earl': 'Libertarian', 'Paul, Ron': 'Republican', 'Santorum, Rick': 'Republican', 'Cain, Herman': 'Republican', 'Gingrich, Newt': 'Republican', 'McCotter, Thaddeus G': 'Republican', 'Huntsman, Jon': 'Republican', 'Perry, Rick': 'Republican' }
讀取文件
In [3]:
table = pd.read_csv('data/usa_election.txt')table.head()
C:\jupyter\lib\site-packages\IPython\core\interactiveshell.py:2785: DtypeWarning: Columns (6) have mixed types. Specify dtype option on import or set low_memory=False. interactivity=interactivity, compiler=compiler, result=result)
Out[3]:
cmte_id cand_id cand_nm contbr_nm contbr_city contbr_st contbr_zip contbr_employer contbr_occupation contb_receipt_amt contb_receipt_dt receipt_desc memo_cd memo_text form_tp file_num
0 C00410118 P20002978 Bachmann, Michelle HARVEY, WILLIAM MOBILE AL 3.6601e 08 RETIRED RETIRED 250.0 20-JUN-11 NaN NaN NaN SA17A 736166
1 C00410118 P20002978 Bachmann, Michelle HARVEY, WILLIAM MOBILE AL 3.6601e 08 RETIRED RETIRED 50.0 23-JUN-11 NaN NaN NaN SA17A 736166
2 C00410118 P20002978 Bachmann, Michelle SMITH, LANIER LANETT AL 3.68633e 08 INFORMATION REQUESTED INFORMATION REQUESTED 250.0 05-JUL-11 NaN NaN NaN SA17A 749073
3 C00410118 P20002978 Bachmann, Michelle BLEVINS, DARONDA PIGGOTT AR 7.24548e 08 NONE RETIRED 250.0 01-AUG-11 NaN NaN NaN SA17A 749073
4 C00410118 P20002978 Bachmann, Michelle WARDENBURG, HAROLD HOT SPRINGS NATION AR 7.19016e 08 NONE RETIRED 300.0 20-JUN-11 NaN NaN NaN SA17A 736166
In [8]:
#使用map函數 字典,新建一列各個候選人所在黨派partytable['party'] = table['cand_nm'].map(parties)table.head()
Out[8]:
cmte_id cand_id cand_nm contbr_nm contbr_city contbr_st contbr_zip contbr_employer contbr_occupation contb_receipt_amt contb_receipt_dt receipt_desc memo_cd memo_text form_tp file_num party
0 C00410118 P20002978 Bachmann, Michelle HARVEY, WILLIAM MOBILE AL 3.6601e 08 RETIRED RETIRED 250.0 20-JUN-11 NaN NaN NaN SA17A 736166 Republican
1 C00410118 P20002978 Bachmann, Michelle HARVEY, WILLIAM MOBILE AL 3.6601e 08 RETIRED RETIRED 50.0 23-JUN-11 NaN NaN NaN SA17A 736166 Republican
2 C00410118 P20002978 Bachmann, Michelle SMITH, LANIER LANETT AL 3.68633e 08 INFORMATION REQUESTED INFORMATION REQUESTED 250.0 05-JUL-11 NaN NaN NaN SA17A 749073 Republican
3 C00410118 P20002978 Bachmann, Michelle BLEVINS, DARONDA PIGGOTT AR 7.24548e 08 NONE RETIRED 250.0 01-AUG-11 NaN NaN NaN SA17A 749073 Republican
4 C00410118 P20002978 Bachmann, Michelle WARDENBURG, HAROLD HOT SPRINGS NATION AR 7.19016e 08 NONE RETIRED 300.0 20-JUN-11 NaN NaN NaN SA17A 736166 Republican
In [10]:
#party這一列中有哪些元素table['party'].unique()
Out[10]:
array(['Republican', 'Democrat', 'Reform', 'Libertarian'], dtype=object)
In [ ]:
#使用value_counts()函數,統計party列中各個元素出現次數,value_counts()是Series中的,無參,返回一個帶有每個元素出現次數的Series
In [11]:
table['party'].value_counts()
Out[11]:
Democrat 292400Republican 237575Reform 5364Libertarian 702Name: party, dtype: int64
In [12]:
#使用groupby()函數,查看各個黨派收到的政治獻金總數contb_receipt_amttable.groupby(by='party')['contb_receipt_amt'].sum()
Out[12]:
partyDemocrat 8.105758e 07Libertarian 4.132769e 05Reform 3.390338e 05Republican 1.192255e 08Name: contb_receipt_amt, dtype: float64
In [13]:
#查看具體每天各個黨派收到的政治獻金總數contb_receipt_amt 。使用groupby([多個分組參數])table.groupby(by=['party','contb_receipt_dt'])['contb_receipt_amt'].sum()
Out[13]:
party contb_receipt_dtDemocrat 01-AUG-11 175281.00 01-DEC-11 651532.82 01-JAN-12 58098.80 01-JUL-11 165961.00 01-JUN-11 145459.00 01-MAY-11 82644.00 01-NOV-11 122529.87 01-OCT-11 148977.00 01-SEP-11 403297.62 02-AUG-11 164510.11 02-DEC-11 216056.96 02-JAN-12 89743.60 02-JUL-11 17105.00 02-JUN-11 422453.00 02-MAY-11 396675.00 02-NOV-11 147183.81 02-OCT-11 62605.62 02-SEP-11 137948.41 03-AUG-11 147053.02 03-DEC-11 81304.02 03-JAN-12 87406.97 03-JUL-11 5982.00 03-JUN-11 320176.20 03-MAY-11 261819.11 03-NOV-11 119304.56 03-OCT-11 363061.02 03-SEP-11 45598.00 04-APR-11 640235.12 04-AUG-11 598784.23 04-DEC-11 72795.10 ... Republican 29-AUG-11 941769.23 29-DEC-11 428501.42 29-JAN-11 750.00 29-JAN-12 75220.02 29-JUL-11 233423.35 29-JUN-11 1340704.29 29-MAR-11 38875.00 29-MAY-11 8363.20 29-NOV-11 407322.64 29-OCT-11 81924.01 29-SEP-11 1612794.52 30-APR-11 43004.80 30-AUG-11 915548.58 30-DEC-11 492470.45 30-JAN-12 255204.80 30-JUL-11 12249.04 30-JUN-11 2744932.63 30-MAR-11 50240.00 30-MAY-11 17803.60 30-NOV-11 809014.83 30-OCT-11 43913.16 30-SEP-11 4886331.76 31-AUG-11 1017735.02 31-DEC-11 1094376.72 31-JAN-11 6000.00 31-JAN-12 869890.41 31-JUL-11 12781.02 31-MAR-11 62475.00 31-MAY-11 301339.80 31-OCT-11 734601.83Name: contb_receipt_amt, Length: 1183, dtype: float64
In [14]:
def trasform_date(d): day,month,year = d.split('-') month = months[month] return "20" year '-' str(month) '-' day
In [17]:
#將表中日期格式轉換為'yyyy-mm-dd'。日期格式,通過函數加map方式進行轉換table['contb_receipt_dt'] = table['contb_receipt_dt'].apply(trasform_date)
In [18]:
table.head()
Out[18]:
cmte_id cand_id cand_nm contbr_nm contbr_city contbr_st contbr_zip contbr_employer contbr_occupation contb_receipt_amt contb_receipt_dt receipt_desc memo_cd memo_text form_tp file_num party
0 C00410118 P20002978 Bachmann, Michelle HARVEY, WILLIAM MOBILE AL 3.6601e 08 RETIRED RETIRED 250.0 2011-6-20 NaN NaN NaN SA17A 736166 Republican
1 C00410118 P20002978 Bachmann, Michelle HARVEY, WILLIAM MOBILE AL 3.6601e 08 RETIRED RETIRED 50.0 2011-6-23 NaN NaN NaN SA17A 736166 Republican
2 C00410118 P20002978 Bachmann, Michelle SMITH, LANIER LANETT AL 3.68633e 08 INFORMATION REQUESTED INFORMATION REQUESTED 250.0 2011-7-05 NaN NaN NaN SA17A 749073 Republican
3 C00410118 P20002978 Bachmann, Michelle BLEVINS, DARONDA PIGGOTT AR 7.24548e 08 NONE RETIRED 250.0 2011-8-01 NaN NaN NaN SA17A 749073 Republican
4 C00410118 P20002978 Bachmann, Michelle WARDENBURG, HAROLD HOT SPRINGS NATION AR 7.19016e 08 NONE RETIRED 300.0 2011-6-20 NaN NaN NaN SA17A 736166 Republican
In [19]:
#查看老兵(捐獻者職業)DISABLED VETERAN主要支持誰 :查看老兵們捐贈給誰的錢最多table['contbr_occupation'] == 'DISABLED VETERAN'
Out[19]:
0 False1 False2 False3 False4 False5 False6 False7 False8 False9 False10 False11 False12 False13 False14 False15 False16 False17 False18 False19 False20 False21 False22 False23 False24 False25 False26 False27 False28 False29 False ... 536011 False536012 False536013 False536014 False536015 False536016 False536017 False536018 False536019 False536020 False536021 False536022 False536023 False536024 False536025 False536026 False536027 False536028 False536029 False536030 False536031 False536032 False536033 False536034 False536035 False536036 False536037 False536038 False536039 False536040 FalseName: contbr_occupation, Length: 536041, dtype: bool
In [21]:
old_bing_df = table.loc[table['contbr_occupation'] == 'DISABLED VETERAN']
In [22]:
old_bing_df.groupby(by='cand_nm')['contb_receipt_amt'].sum()
Out[22]:
cand_nmCain, Herman 300.00Obama, Barack 4205.00Paul, Ron 2425.49Santorum, Rick 250.00Name: contb_receipt_amt, dtype: float64
In [23]:
table['contb_receipt_amt'].max()
Out[23]:
1944042.43
In [24]:
#找出候選人的捐贈者中,捐贈金額最大的人的職業以及捐獻額 .通過query("查詢條件來查找捐獻人職業")table.query('contb_receipt_amt == 1944042.43')
Out[24]:
cmte_id cand_id cand_nm contbr_nm contbr_city contbr_st contbr_zip contbr_employer contbr_occupation contb_receipt_amt contb_receipt_dt receipt_desc memo_cd memo_text form_tp file_num party
176127 C00431445 P80003338 Obama, Barack OBAMA VICTORY FUND 2012 - UNITEMIZED CHICAGO IL 60680 NaN NaN 1944042.43 2011-12-31 NaN X * SA18 763233 Democrat
來源:https://www.icode9.com/content-1-497751.html