# 打印所有结果
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
import pandas as pd
1. 从文件读
# 【1】从剪切板上读取 - 方便,适用于演示、数据量小的数据读取
BSdata = pd.read_clipboard();
BSdata
Region/Country/Area | Unnamed: 1 | Year | Series | Value | Footnotes | |
---|---|---|---|---|---|---|
0 | 1 | Total, all countries or areas | 2010 | Population mid-year estimates (millions) | 6,956.82 | NaN |
1 | 1 | Total, all countries or areas | 2010 | Population mid-year estimates for males (milli... | 3,507.70 | NaN |
2 | 1 | Total, all countries or areas | 2010 | Population mid-year estimates for females (mil... | 3,449.12 | NaN |
3 | 1 | Total, all countries or areas | 2010 | Sex ratio (males per 100 females) | 101.7 | NaN |
4 | 1 | Total, all countries or areas | 2010 | Population aged 0 to 14 years old (percentage) | 27 | NaN |
5 | 1 | Total, all countries or areas | 2010 | Population aged 60+ years old (percentage) | 11 | NaN |
6 | 1 | Total, all countries or areas | 2010 | Population density | 53.5 | NaN |
7 | 1 | Total, all countries or areas | 2015 | Population mid-year estimates (millions) | 7,379.80 | NaN |
8 | 1 | Total, all countries or areas | 2015 | Population mid-year estimates for males (milli... | 3,720.70 | NaN |
9 | 1 | Total, all countries or areas | 2015 | Population mid-year estimates for females (mil... | 3,659.10 | NaN |
10 | 1 | Total, all countries or areas | 2015 | Sex ratio (males per 100 females) | 101.7 | NaN |
11 | 1 | Total, all countries or areas | 2015 | Population aged 0 to 14 years old (percentage) | 26.2 | NaN |
12 | 1 | Total, all countries or areas | 2015 | Population aged 60+ years old (percentage) | 12.2 | NaN |
13 | 1 | Total, all countries or areas | 2015 | Population density | 56.7 | NaN |
14 | 1 | Total, all countries or areas | 2015 | Surface area (thousand km2) | 136,162 | NaN |
15 | 1 | Total, all countries or areas | 2019 | Population mid-year estimates (millions) | 7,713.47 | NaN |
16 | 1 | Total, all countries or areas | 2019 | Population mid-year estimates for males (milli... | 3,889.03 | NaN |
# 【2】读取 csv 数据 - csv是文本文件,以逗号做分隔 - 常用
BSdata = pd.read_csv("data/test.csv", encoding="utf-8") #GBK
BSdata.head() # head()取数据头5条
Region/Country/Area | Unnamed: 1 | Year | Series | Value | Footnotes | Source | |
---|---|---|---|---|---|---|---|
0 | 1.0 | Total, all countries or areas | 2010.0 | Population mid-year estimates (millions) | 6,956.82 | NaN | United Nations Population Division, New York, ... |
1 | 1.0 | Total, all countries or areas | 2010.0 | Population mid-year estimates for males (milli... | 3,507.70 | NaN | United Nations Population Division, New York, ... |
2 | 1.0 | Total, all countries or areas | 2010.0 | Population mid-year estimates for females (mil... | 3,449.12 | NaN | United Nations Population Division, New York, ... |
3 | 1.0 | Total, all countries or areas | 2010.0 | Sex ratio (males per 100 females) | 101.7 | NaN | United Nations Population Division, New York, ... |
4 | 1.0 | Total, all countries or areas | 2010.0 | Population aged 0 to 14 years old (percentage) | 27 | NaN | United Nations Population Division, New York, ... |
# 【3】读取 Excel 格式数据 - 推荐!! 中英文识别好
BSdata = pd.read_excel("data/test.xlsx", 'BSdata') # 读取test.xlsx中BSdata表单中的数据
BSdata
Region/Country/Area | Unnamed: 1 | Year | Series | Value | Footnotes | Source | |
---|---|---|---|---|---|---|---|
0 | 1 | Total, all countries or areas | 2010 | Population mid-year estimates (millions) | 6956.82 | NaN | United Nations Population Division, New York, ... |
1 | 1 | Total, all countries or areas | 2010 | Population mid-year estimates for males (milli... | 3507.70 | NaN | United Nations Population Division, New York, ... |
2 | 1 | Total, all countries or areas | 2010 | Population mid-year estimates for females (mil... | 3449.12 | NaN | United Nations Population Division, New York, ... |
3 | 1 | Total, all countries or areas | 2010 | Sex ratio (males per 100 females) | 101.70 | NaN | United Nations Population Division, New York, ... |
4 | 1 | Total, all countries or areas | 2010 | Population aged 0 to 14 years old (percentage) | 27.00 | NaN | United Nations Population Division, New York, ... |
5 | 1 | Total, all countries or areas | 2010 | Population aged 60+ years old (percentage) | 11.00 | NaN | United Nations Population Division, New York, ... |
6 | 1 | Total, all countries or areas | 2010 | Population density | 53.50 | NaN | United Nations Population Division, New York, ... |
7 | 1 | Total, all countries or areas | 2015 | Population mid-year estimates (millions) | 7379.80 | NaN | United Nations Population Division, New York, ... |
8 | 1 | Total, all countries or areas | 2015 | Population mid-year estimates for males (milli... | 3720.70 | NaN | United Nations Population Division, New York, ... |
9 | 1 | Total, all countries or areas | 2015 | Population mid-year estimates for females (mil... | 3659.10 | NaN | United Nations Population Division, New York, ... |
10 | 1 | Total, all countries or areas | 2015 | Sex ratio (males per 100 females) | 101.70 | NaN | United Nations Population Division, New York, ... |
11 | 1 | Total, all countries or areas | 2015 | Population aged 0 to 14 years old (percentage) | 26.20 | NaN | United Nations Population Division, New York, ... |
12 | 1 | Total, all countries or areas | 2015 | Population aged 60+ years old (percentage) | 12.20 | NaN | United Nations Population Division, New York, ... |
13 | 1 | Total, all countries or areas | 2015 | Population density | 56.70 | NaN | United Nations Population Division, New York, ... |
14 | 1 | Total, all countries or areas | 2015 | Surface area (thousand km2) | 136162.00 | NaN | United Nations Statistics Division, New York, ... |
15 | 1 | Total, all countries or areas | 2019 | Population mid-year estimates (millions) | 7713.47 | NaN | United Nations Population Division, New York, ... |
16 | 1 | Total, all countries or areas | 2019 | Population mid-year estimates for males (milli... | 3889.03 | NaN | United Nations Population Division, New York, ... |
17 | 1 | Total, all countries or areas | 2019 | Population mid-year estimates for females (mil... | 3824.43 | NaN | United Nations Population Division, New York, ... |
18 | 1 | Total, all countries or areas | 2019 | Sex ratio (males per 100 females) | 101.70 | NaN | United Nations Population Division, New York, ... |
19 | 1 | Total, all countries or areas | 2019 | Population aged 0 to 14 years old (percentage) | 25.60 | NaN | United Nations Population Division, New York, ... |
20 | 1 | Total, all countries or areas | 2019 | Population aged 60+ years old (percentage) | 13.20 | NaN | United Nations Population Division, New York, ... |
21 | 1 | Total, all countries or areas | 2019 | Population density | 59.30 | NaN | United Nations Population Division, New York, ... |
22 | 1 | Total, all countries or areas | 2019 | Surface area (thousand km2) | 130094.00 | NaN | United Nations Statistics Division, New York, ... |
23 | 1 | Total, all countries or areas | 2021 | Population mid-year estimates (millions) | 7874.97 | Projected estimate (medium fertility variant). | United Nations Population Division, New York, ... |
24 | 1 | Total, all countries or areas | 2021 | Population mid-year estimates for males (milli... | 3970.24 | Projected estimate (medium fertility variant). | United Nations Population Division, New York, ... |
2. 保存数据到文件
# 【1】保存为csv格式
BSdata.to_csv('data/BSdata.csv')
# 【2】保存到Excel文件中
BSdata.to_excel('data/BSdata.xlsx',index = False)# index=False 表示不保存行标签 - 默认为True
标签:...,countries,NaN,Population,Total,Pandas,areas
From: https://www.cnblogs.com/luna2333/p/16735683.html