```python
import pymysql
```
```python
conn = pymysql.connect(
host = 'localhost', #127.0.0.1,主机
user = 'root',
password = '6129w6851',
db = 'world',
port = 3306,
charset = 'utf8'
) #连接数据库
```
```python
cur = conn.cursor() #连接数据库之后要必须要设的参数,创建游标
```
```python
cur.execute('select * from country')
```
239
```python
data = cur.fetchall() #把数据放入data
```
```python
for d in data:
print(d[0],d[1],d[4])
```
ABW Aruba 193.0
AFG Afghanistan 652090.0
AGO Angola 1246700.0
AIA Anguilla 96.0
ALB Albania 28748.0
AND Andorra 468.0
ANT Netherlands Antilles 800.0
ARE United Arab Emirates 83600.0
ARG Argentina 2780400.0
ARM Armenia 29800.0
ASM American Samoa 199.0
ATA Antarctica 13120000.0
ATF French Southern territories 7780.0
ATG Antigua and Barbuda 442.0
AUS Australia 7741220.0
AUT Austria 83859.0
AZE Azerbaijan 86600.0
BDI Burundi 27834.0
BEL Belgium 30518.0
BEN Benin 112622.0
BFA Burkina Faso 274000.0
BGD Bangladesh 143998.0
BGR Bulgaria 110994.0
BHR Bahrain 694.0
BHS Bahamas 13878.0
BIH Bosnia and Herzegovina 51197.0
BLR Belarus 207600.0
BLZ Belize 22696.0
BMU Bermuda 53.0
BOL Bolivia 1098581.0
BRA Brazil 8547403.0
BRB Barbados 430.0
BRN Brunei 5765.0
BTN Bhutan 47000.0
BVT Bouvet Island 59.0
BWA Botswana 581730.0
CAF Central African Republic 622984.0
CAN Canada 9970610.0
CCK Cocos (Keeling) Islands 14.0
CHE Switzerland 41284.0
CHL Chile 756626.0
CHN China 9572900.0
CIV Côte d’Ivoire 322463.0
CMR Cameroon 475442.0
COD Congo, The Democratic Republic of the 2344858.0
COG Congo 342000.0
COK Cook Islands 236.0
COL Colombia 1138914.0
COM Comoros 1862.0
CPV Cape Verde 4033.0
CRI Costa Rica 51100.0
CUB Cuba 110861.0
CXR Christmas Island 135.0
CYM Cayman Islands 264.0
CYP Cyprus 9251.0
CZE Czech Republic 78866.0
DEU Germany 357022.0
DJI Djibouti 23200.0
DMA Dominica 751.0
DNK Denmark 43094.0
DOM Dominican Republic 48511.0
DZA Algeria 2381741.0
ECU Ecuador 283561.0
EGY Egypt 1001449.0
ERI Eritrea 117600.0
ESH Western Sahara 266000.0
ESP Spain 505992.0
EST Estonia 45227.0
ETH Ethiopia 1104300.0
FIN Finland 338145.0
FJI Fiji Islands 18274.0
FLK Falkland Islands 12173.0
FRA France 551500.0
FRO Faroe Islands 1399.0
FSM Micronesia, Federated States of 702.0
GAB Gabon 267668.0
GBR United Kingdom 242900.0
GEO Georgia 69700.0
GHA Ghana 238533.0
GIB Gibraltar 6.0
GIN Guinea 245857.0
GLP Guadeloupe 1705.0
GMB Gambia 11295.0
GNB Guinea-Bissau 36125.0
GNQ Equatorial Guinea 28051.0
GRC Greece 131626.0
GRD Grenada 344.0
GRL Greenland 2166090.0
GTM Guatemala 108889.0
GUF French Guiana 90000.0
GUM Guam 549.0
GUY Guyana 214969.0
HKG Hong Kong 1075.0
HMD Heard Island and McDonald Islands 359.0
HND Honduras 112088.0
HRV Croatia 56538.0
HTI Haiti 27750.0
HUN Hungary 93030.0
IDN Indonesia 1904569.0
IND India 3287263.0
IOT British Indian Ocean Territory 78.0
IRL Ireland 70273.0
IRN Iran 1648195.0
IRQ Iraq 438317.0
ISL Iceland 103000.0
ISR Israel 21056.0
ITA Italy 301316.0
JAM Jamaica 10990.0
JOR Jordan 88946.0
JPN Japan 377829.0
KAZ Kazakstan 2724900.0
KEN Kenya 580367.0
KGZ Kyrgyzstan 199900.0
KHM Cambodia 181035.0
KIR Kiribati 726.0
KNA Saint Kitts and Nevis 261.0
KOR South Korea 99434.0
KWT Kuwait 17818.0
LAO Laos 236800.0
LBN Lebanon 10400.0
LBR Liberia 111369.0
LBY Libyan Arab Jamahiriya 1759540.0
LCA Saint Lucia 622.0
LIE Liechtenstein 160.0
LKA Sri Lanka 65610.0
LSO Lesotho 30355.0
LTU Lithuania 65301.0
LUX Luxembourg 2586.0
LVA Latvia 64589.0
MAC Macao 18.0
MAR Morocco 446550.0
MCO Monaco 1.5
MDA Moldova 33851.0
MDG Madagascar 587041.0
MDV Maldives 298.0
MEX Mexico 1958201.0
MHL Marshall Islands 181.0
MKD Macedonia 25713.0
MLI Mali 1240192.0
MLT Malta 316.0
MMR Myanmar 676578.0
MNG Mongolia 1566500.0
MNP Northern Mariana Islands 464.0
MOZ Mozambique 801590.0
MRT Mauritania 1025520.0
MSR Montserrat 102.0
MTQ Martinique 1102.0
MUS Mauritius 2040.0
MWI Malawi 118484.0
MYS Malaysia 329758.0
MYT Mayotte 373.0
NAM Namibia 824292.0
NCL New Caledonia 18575.0
NER Niger 1267000.0
NFK Norfolk Island 36.0
NGA Nigeria 923768.0
NIC Nicaragua 130000.0
NIU Niue 260.0
NLD Netherlands 41526.0
NOR Norway 323877.0
NPL Nepal 147181.0
NRU Nauru 21.0
NZL New Zealand 270534.0
OMN Oman 309500.0
PAK Pakistan 796095.0
PAN Panama 75517.0
PCN Pitcairn 49.0
PER Peru 1285216.0
PHL Philippines 300000.0
PLW Palau 459.0
PNG Papua New Guinea 462840.0
POL Poland 323250.0
PRI Puerto Rico 8875.0
PRK North Korea 120538.0
PRT Portugal 91982.0
PRY Paraguay 406752.0
PSE Palestine 6257.0
PYF French Polynesia 4000.0
QAT Qatar 11000.0
REU Réunion 2510.0
ROM Romania 238391.0
RUS Russian Federation 17075400.0
RWA Rwanda 26338.0
SAU Saudi Arabia 2149690.0
SDN Sudan 2505813.0
SEN Senegal 196722.0
SGP Singapore 618.0
SGS South Georgia and the South Sandwich Islands 3903.0
SHN Saint Helena 314.0
SJM Svalbard and Jan Mayen 62422.0
SLB Solomon Islands 28896.0
SLE Sierra Leone 71740.0
SLV El Salvador 21041.0
SMR San Marino 61.0
SOM Somalia 637657.0
SPM Saint Pierre and Miquelon 242.0
STP Sao Tome and Principe 964.0
SUR Suriname 163265.0
SVK Slovakia 49012.0
SVN Slovenia 20256.0
SWE Sweden 449964.0
SWZ Swaziland 17364.0
SYC Seychelles 455.0
SYR Syria 185180.0
TCA Turks and Caicos Islands 430.0
TCD Chad 1284000.0
TGO Togo 56785.0
THA Thailand 513115.0
TJK Tajikistan 143100.0
TKL Tokelau 12.0
TKM Turkmenistan 488100.0
TMP East Timor 14874.0
TON Tonga 650.0
TTO Trinidad and Tobago 5130.0
TUN Tunisia 163610.0
TUR Turkey 774815.0
TUV Tuvalu 26.0
TWN Taiwan 36188.0
TZA Tanzania 883749.0
UGA Uganda 241038.0
UKR Ukraine 603700.0
UMI United States Minor Outlying Islands 16.0
URY Uruguay 175016.0
USA United States 9363520.0
UZB Uzbekistan 447400.0
VAT Holy See (Vatican City State) 0.4
VCT Saint Vincent and the Grenadines 388.0
VEN Venezuela 912050.0
VGB Virgin Islands, British 151.0
VIR Virgin Islands, U.S. 347.0
VNM Vietnam 331689.0
VUT Vanuatu 12189.0
WLF Wallis and Futuna 200.0
WSM Samoa 2831.0
YEM Yemen 527968.0
YUG Yugoslavia 102173.0
ZAF South Africa 1221037.0
ZMB Zambia 752618.0
ZWE Zimbabwe 390757.0
```python
cur.execute("select * from country where Code = 'AIA' or Code = 'DOM' ") #在筛选的时候就加入限定条件
```
2
```python
data_2 = cur.fetchall()
```
```python
for d in data_2:
print(d[0],d[1],d[4])
```
AIA Anguilla 96.0
DOM Dominican Republic 48511.0
```python
conn.commit() #将做出的修改上传保存
```
```python
cur.close()
conn.close() #把数据库关闭,避免出错
```
```python
import pandas as pd
from sqlalchemy import create_engine #OIM框架,帮助我们读写
```
```python
sql = "select * from country where population > 100000"
engine = create_engine('mysql+pymysql://root:6129w6851@localhost:3306/world?charset=utf8')
df = pd.read_sql(sql,engine)
```
d:\ProgramData\Anaconda3\lib\site-packages\pymysql\cursors.py:166: Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'VARIABLE_VALUE' at row 496")
result = self._query(query)
d:\ProgramData\Anaconda3\lib\site-packages\pymysql\cursors.py:166: Warning: (1287, "'@@tx_isolation' is deprecated and will be removed in a future release. Please use '@@transaction_isolation' instead")
result = self._query(query)
```python
df
```
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
Code | Name | Continent | Region | SurfaceArea | IndepYear | Population | LifeExpectancy | GNP | GNPOld | LocalName | GovernmentForm | HeadOfState | Capital | Code2 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | ABW | Aruba | North America | Caribbean | 193.0 | NaN | 103000 | 78.4 | 828.0 | 793.0 | Aruba | Nonmetropolitan Territory of The Netherlands | Beatrix | 129 | AW |
1 | AFG | Afghanistan | Asia | Southern and Central Asia | 652090.0 | 1919.0 | 22720000 | 45.9 | 5976.0 | NaN | Afganistan/Afqanestan | Islamic Emirate | Mohammad Omar | 1 | AF |
2 | AGO | Angola | Africa | Central Africa | 1246700.0 | 1975.0 | 12878000 | 38.3 | 6648.0 | 7984.0 | Angola | Republic | José Eduardo dos Santos | 56 | AO |
3 | ALB | Albania | Europe | Southern Europe | 28748.0 | 1912.0 | 3401200 | 71.6 | 3205.0 | 2500.0 | Shqipëria | Republic | Rexhep Mejdani | 34 | AL |
4 | ANT | Netherlands Antilles | North America | Caribbean | 800.0 | NaN | 217000 | 74.7 | 1941.0 | NaN | Nederlandse Antillen | Nonmetropolitan Territory of The Netherlands | Beatrix | 33 | AN |
5 | ARE | United Arab Emirates | Asia | Middle East | 83600.0 | 1971.0 | 2441000 | 74.1 | 37966.0 | 36846.0 | Al-Imarat al-´Arabiya al-Muttahida | Emirate Federation | Zayid bin Sultan al-Nahayan | 65 | AE |
6 | ARG | Argentina | South America | South America | 2780400.0 | 1816.0 | 37032000 | 75.1 | 340238.0 | 323310.0 | Argentina | Federal Republic | Fernando de la Rúa | 69 | AR |
7 | ARM | Armenia | Asia | Middle East | 29800.0 | 1991.0 | 3520000 | 66.4 | 1813.0 | 1627.0 | Hajastan | Republic | Robert Kotšarjan | 126 | AM |
8 | AUS | Australia | Oceania | Australia and New Zealand | 7741220.0 | 1901.0 | 18886000 | 79.8 | 351182.0 | 392911.0 | Australia | Constitutional Monarchy, Federation | Elisabeth II | 135 | AU |
9 | AUT | Austria | Europe | Western Europe | 83859.0 | 1918.0 | 8091800 | 77.7 | 211860.0 | 206025.0 | Österreich | Federal Republic | Thomas Klestil | 1523 | AT |
10 | AZE | Azerbaijan | Asia | Middle East | 86600.0 | 1991.0 | 7734000 | 62.9 | 4127.0 | 4100.0 | Azärbaycan | Federal Republic | Heydär Äliyev | 144 | AZ |
11 | BDI | Burundi | Africa | Eastern Africa | 27834.0 | 1962.0 | 6695000 | 46.2 | 903.0 | 982.0 | Burundi/Uburundi | Republic | Pierre Buyoya | 552 | BI |
12 | BEL | Belgium | Europe | Western Europe | 30518.0 | 1830.0 | 10239000 | 77.8 | 249704.0 | 243948.0 | België/Belgique | Constitutional Monarchy, Federation | Albert II | 179 | BE |
13 | BEN | Benin | Africa | Western Africa | 112622.0 | 1960.0 | 6097000 | 50.2 | 2357.0 | 2141.0 | Bénin | Republic | Mathieu Kérékou | 187 | BJ |
14 | BFA | Burkina Faso | Africa | Western Africa | 274000.0 | 1960.0 | 11937000 | 46.7 | 2425.0 | 2201.0 | Burkina Faso | Republic | Blaise Compaoré | 549 | BF |
15 | BGD | Bangladesh | Asia | Southern and Central Asia | 143998.0 | 1971.0 | 129155000 | 60.2 | 32852.0 | 31966.0 | Bangladesh | Republic | Shahabuddin Ahmad | 150 | BD |
16 | BGR | Bulgaria | Europe | Eastern Europe | 110994.0 | 1908.0 | 8190900 | 70.9 | 12178.0 | 10169.0 | Balgarija | Republic | Petar Stojanov | 539 | BG |
17 | BHR | Bahrain | Asia | Middle East | 694.0 | 1971.0 | 617000 | 73.0 | 6366.0 | 6097.0 | Al-Bahrayn | Monarchy (Emirate) | Hamad ibn Isa al-Khalifa | 149 | BH |
18 | BHS | Bahamas | North America | Caribbean | 13878.0 | 1973.0 | 307000 | 71.1 | 3527.0 | 3347.0 | The Bahamas | Constitutional Monarchy | Elisabeth II | 148 | BS |
19 | BIH | Bosnia and Herzegovina | Europe | Southern Europe | 51197.0 | 1992.0 | 3972000 | 71.5 | 2841.0 | NaN | Bosna i Hercegovina | Federal Republic | Ante Jelavic | 201 | BA |
20 | BLR | Belarus | Europe | Eastern Europe | 207600.0 | 1991.0 | 10236000 | 68.0 | 13714.0 | NaN | Belarus | Republic | Aljaksandr Lukašenka | 3520 | BY |
21 | BLZ | Belize | North America | Central America | 22696.0 | 1981.0 | 241000 | 70.9 | 630.0 | 616.0 | Belize | Constitutional Monarchy | Elisabeth II | 185 | BZ |
22 | BOL | Bolivia | South America | South America | 1098581.0 | 1825.0 | 8329000 | 63.7 | 8571.0 | 7967.0 | Bolivia | Republic | Hugo Bánzer Suárez | 194 | BO |
23 | BRA | Brazil | South America | South America | 8547403.0 | 1822.0 | 170115000 | 62.9 | 776739.0 | 804108.0 | Brasil | Federal Republic | Fernando Henrique Cardoso | 211 | BR |
24 | BRB | Barbados | North America | Caribbean | 430.0 | 1966.0 | 270000 | 73.0 | 2223.0 | 2186.0 | Barbados | Constitutional Monarchy | Elisabeth II | 174 | BB |
25 | BRN | Brunei | Asia | Southeast Asia | 5765.0 | 1984.0 | 328000 | 73.6 | 11705.0 | 12460.0 | Brunei Darussalam | Monarchy (Sultanate) | Haji Hassan al-Bolkiah | 538 | BN |
26 | BTN | Bhutan | Asia | Southern and Central Asia | 47000.0 | 1910.0 | 2124000 | 52.4 | 372.0 | 383.0 | Druk-Yul | Monarchy | Jigme Singye Wangchuk | 192 | BT |
27 | BWA | Botswana | Africa | Southern Africa | 581730.0 | 1966.0 | 1622000 | 39.3 | 4834.0 | 4935.0 | Botswana | Republic | Festus G. Mogae | 204 | BW |
28 | CAF | Central African Republic | Africa | Central Africa | 622984.0 | 1960.0 | 3615000 | 44.0 | 1054.0 | 993.0 | Centrafrique/Bê-Afrîka | Republic | Ange-Félix Patassé | 1889 | CF |
29 | CAN | Canada | North America | North America | 9970610.0 | 1867.0 | 31147000 | 79.4 | 598862.0 | 625626.0 | Canada | Constitutional Monarchy, Federation | Elisabeth II | 1822 | CA |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
162 | SVN | Slovenia | Europe | Southern Europe | 20256.0 | 1991.0 | 1987800 | 74.9 | 19756.0 | 18202.0 | Slovenija | Republic | Milan Kucan | 3212 | SI |
163 | SWE | Sweden | Europe | Nordic Countries | 449964.0 | 836.0 | 8861400 | 79.6 | 226492.0 | 227757.0 | Sverige | Constitutional Monarchy | Carl XVI Gustaf | 3048 | SE |
164 | SWZ | Swaziland | Africa | Southern Africa | 17364.0 | 1968.0 | 1008000 | 40.4 | 1206.0 | 1312.0 | kaNgwane | Monarchy | Mswati III | 3244 | SZ |
165 | SYR | Syria | Asia | Middle East | 185180.0 | 1941.0 | 16125000 | 68.5 | 65984.0 | 64926.0 | Suriya | Republic | Bashar al-Assad | 3250 | SY |
166 | TCD | Chad | Africa | Central Africa | 1284000.0 | 1960.0 | 7651000 | 50.5 | 1208.0 | 1102.0 | Tchad/Tshad | Republic | Idriss Déby | 3337 | TD |
167 | TGO | Togo | Africa | Western Africa | 56785.0 | 1960.0 | 4629000 | 54.7 | 1449.0 | 1400.0 | Togo | Republic | Gnassingbé Eyadéma | 3332 | TG |
168 | THA | Thailand | Asia | Southeast Asia | 513115.0 | 1350.0 | 61399000 | 68.6 | 116416.0 | 153907.0 | Prathet Thai | Constitutional Monarchy | Bhumibol Adulyadej | 3320 | TH |
169 | TJK | Tajikistan | Asia | Southern and Central Asia | 143100.0 | 1991.0 | 6188000 | 64.1 | 1990.0 | 1056.0 | Toçikiston | Republic | Emomali Rahmonov | 3261 | TJ |
170 | TKM | Turkmenistan | Asia | Southern and Central Asia | 488100.0 | 1991.0 | 4459000 | 60.9 | 4397.0 | 2000.0 | Türkmenostan | Republic | Saparmurad Nijazov | 3419 | TM |
171 | TMP | East Timor | Asia | Southeast Asia | 14874.0 | NaN | 885000 | 46.0 | 0.0 | NaN | Timor Timur | Administrated by the UN | José Alexandre Gusmão | 1522 | TP |
172 | TTO | Trinidad and Tobago | North America | Caribbean | 5130.0 | 1962.0 | 1295000 | 68.0 | 6232.0 | 5867.0 | Trinidad and Tobago | Republic | Arthur N. R. Robinson | 3336 | TT |
173 | TUN | Tunisia | Africa | Northern Africa | 163610.0 | 1956.0 | 9586000 | 73.7 | 20026.0 | 18898.0 | Tunis/Tunisie | Republic | Zine al-Abidine Ben Ali | 3349 | TN |
174 | TUR | Turkey | Asia | Middle East | 774815.0 | 1923.0 | 66591000 | 71.0 | 210721.0 | 189122.0 | Türkiye | Republic | Ahmet Necdet Sezer | 3358 | TR |
175 | TWN | Taiwan | Asia | Eastern Asia | 36188.0 | 1945.0 | 22256000 | 76.4 | 256254.0 | 263451.0 | TÂ’ai-wan | Republic | Chen Shui-bian | 3263 | TW |
176 | TZA | Tanzania | Africa | Eastern Africa | 883749.0 | 1961.0 | 33517000 | 52.3 | 8005.0 | 7388.0 | Tanzania | Republic | Benjamin William Mkapa | 3306 | TZ |
177 | UGA | Uganda | Africa | Eastern Africa | 241038.0 | 1962.0 | 21778000 | 42.9 | 6313.0 | 6887.0 | Uganda | Republic | Yoweri Museveni | 3425 | UG |
178 | UKR | Ukraine | Europe | Eastern Europe | 603700.0 | 1991.0 | 50456000 | 66.0 | 42168.0 | 49677.0 | Ukrajina | Republic | Leonid Kutšma | 3426 | UA |
179 | URY | Uruguay | South America | South America | 175016.0 | 1828.0 | 3337000 | 75.2 | 20831.0 | 19967.0 | Uruguay | Republic | Jorge Batlle Ibáñez | 3492 | UY |
180 | USA | United States | North America | North America | 9363520.0 | 1776.0 | 278357000 | 77.1 | 8510700.0 | 8110900.0 | United States | Federal Republic | George W. Bush | 3813 | US |
181 | UZB | Uzbekistan | Asia | Southern and Central Asia | 447400.0 | 1991.0 | 24318000 | 63.7 | 14194.0 | 21300.0 | Uzbekiston | Republic | Islam Karimov | 3503 | UZ |
182 | VCT | Saint Vincent and the Grenadines | North America | Caribbean | 388.0 | 1979.0 | 114000 | 72.3 | 285.0 | NaN | Saint Vincent and the Grenadines | Constitutional Monarchy | Elisabeth II | 3066 | VC |
183 | VEN | Venezuela | South America | South America | 912050.0 | 1811.0 | 24170000 | 73.1 | 95023.0 | 88434.0 | Venezuela | Federal Republic | Hugo Chávez FrÃas | 3539 | VE |
184 | VNM | Vietnam | Asia | Southeast Asia | 331689.0 | 1945.0 | 79832000 | 69.3 | 21929.0 | 22834.0 | Viêt Nam | Socialistic Republic | Trân Duc Luong | 3770 | VN |
185 | VUT | Vanuatu | Oceania | Melanesia | 12189.0 | 1980.0 | 190000 | 60.6 | 261.0 | 246.0 | Vanuatu | Republic | John Bani | 3537 | VU |
186 | WSM | Samoa | Oceania | Polynesia | 2831.0 | 1962.0 | 180000 | 69.2 | 141.0 | 157.0 | Samoa | Parlementary Monarchy | Malietoa Tanumafili II | 3169 | WS |
187 | YEM | Yemen | Asia | Middle East | 527968.0 | 1918.0 | 18112000 | 59.8 | 6041.0 | 5729.0 | Al-Yaman | Republic | Ali Abdallah Salih | 1780 | YE |
188 | YUG | Yugoslavia | Europe | Southern Europe | 102173.0 | 1918.0 | 10640000 | 72.4 | 17000.0 | NaN | Jugoslavija | Federal Republic | Vojislav Koštunica | 1792 | YU |
189 | ZAF | South Africa | Africa | Southern Africa | 1221037.0 | 1910.0 | 40377000 | 51.1 | 116729.0 | 129092.0 | South Africa | Republic | Thabo Mbeki | 716 | ZA |
190 | ZMB | Zambia | Africa | Eastern Africa | 752618.0 | 1964.0 | 9169000 | 37.2 | 3377.0 | 3922.0 | Zambia | Republic | Frederick Chiluba | 3162 | ZM |
191 | ZWE | Zimbabwe | Africa | Eastern Africa | 390757.0 | 1980.0 | 11669000 | 37.8 | 5951.0 | 8670.0 | Zimbabwe | Republic | Robert G. Mugabe | 4068 | ZW |
192 rows × 15 columns
parse_dates 把某个字段转成时间类型 ,data_parser 具体的时间类型(pd.read,在读取时操作)
columns = ['列1','列2','列3','列4']
df = pd.read_table('文件.txt',names = columns,sep='\s+',parse_dates=,date_parser=)
```python
df['month']=20171209
```
```python
df.info() #month是int格式,是数字
```
RangeIndex: 192 entries, 0 to 191
Data columns (total 16 columns):
Code 192 non-null object
Name 192 non-null object
Continent 192 non-null object
Region 192 non-null object
SurfaceArea 192 non-null float64
IndepYear 176 non-null float64
Population 192 non-null int64
LifeExpectancy 192 non-null float64
GNP 192 non-null float64
GNPOld 170 non-null float64
LocalName 192 non-null object
GovernmentForm 192 non-null object
HeadOfState 192 non-null object
Capital 192 non-null int64
Code2 192 non-null object
month 192 non-null int64
dtypes: float64(5), int64(3), object(8)
memory usage: 24.1+ KB
```python
df['month'] = pd.to_datetime(df.month,format="%Y%m%d") #把数据类型解析转成时间,要转换的数据+format方法
df['date'] = df.month.values.astype('datetime64[M]') #先加个values,变成数组形式,然后再用数组的功能转换数据。
```
```python
df.info() #month变为时间格式数据,精度ns纳秒
```
RangeIndex: 192 entries, 0 to 191
Data columns (total 17 columns):
Code 192 non-null object
Name 192 non-null object
Continent 192 non-null object
Region 192 non-null object
SurfaceArea 192 non-null float64
IndepYear 176 non-null float64
Population 192 non-null int64
LifeExpectancy 192 non-null float64
GNP 192 non-null float64
GNPOld 170 non-null float64
LocalName 192 non-null object
GovernmentForm 192 non-null object
HeadOfState 192 non-null object
Capital 192 non-null int64
Code2 192 non-null object
month 192 non-null datetime64[ns]
date 192 non-null datetime64[ns]
dtypes: datetime64[ns](2), float64(5), int64(2), object(8)
memory usage: 25.6+ KB
```python
result = df.groupby(['GovernmentForm']).count()['Code'].reset_index() #数据透视表
```
```python
result
```
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
GovernmentForm | Code | |
---|---|---|
0 | Administrated by the UN | 1 |
1 | Autonomous Area | 1 |
2 | Commonwealth of the US | 1 |
3 | Constitutional Monarchy | 23 |
4 | Constitutional Monarchy (Emirate) | 1 |
5 | Constitutional Monarchy, Federation | 4 |
6 | Emirate Federation | 1 |
7 | Federal Republic | 15 |
8 | Federation | 1 |
9 | Islamic Emirate | 1 |
10 | Islamic Republic | 2 |
11 | Monarchy | 4 |
12 | Monarchy (Emirate) | 1 |
13 | Monarchy (Sultanate) | 2 |
14 | Nonmetropolitan Territory of France | 2 |
15 | Nonmetropolitan Territory of The Netherlands | 2 |
16 | Occupied by Marocco | 1 |
17 | Overseas Department of France | 4 |
18 | Parlementary Monarchy | 1 |
19 | People'sRepublic | 1 |
20 | Republic | 115 |
21 | Socialistic Republic | 3 |
22 | Socialistic State | 1 |
23 | Special Administrative Region of China | 2 |
24 | Territorial Collectivity of France | 1 |
25 | US Territory | 1 |
```python
result.to_sql(name = 'newtable',con= 'mysql+pymysql://root:6129w6851@localhost:3306/world?charset=utf8',
if_exists = 'append', index = False) #不去数据库,直接创建表(表会比较占内存,字段可能有问题)
```
d:\ProgramData\Anaconda3\lib\site-packages\pymysql\cursors.py:166: Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'VARIABLE_VALUE' at row 496")
result = self._query(query)
d:\ProgramData\Anaconda3\lib\site-packages\pymysql\cursors.py:166: Warning: (1287, "'@@tx_isolation' is deprecated and will be removed in a future release. Please use '@@transaction_isolation' instead")
result = self._query(query)
```python
result.to_sql(name = 'newtable2',con= 'mysql+pymysql://root:6129w6851@localhost:3306/world?charset=utf8',
if_exists = 'append', index = False) #在数据库建好表的属性,再插入数据(建议方式)
```
d:\ProgramData\Anaconda3\lib\site-packages\pymysql\cursors.py:166: Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'VARIABLE_VALUE' at row 496")
result = self._query(query)
d:\ProgramData\Anaconda3\lib\site-packages\pymysql\cursors.py:166: Warning: (1287, "'@@tx_isolation' is deprecated and will be removed in a future release. Please use '@@transaction_isolation' instead")
result = self._query(query)
** 创建表的时候字段有三个,但写入时,只写入两个。会出现什么情况?**
少写会空值,多写会报错。