dic_v1
= {"confirm":500,"heal":480}
print(dic_v1
)
print(type(dic_v1
))
print("将字典dic_v1转换为字符串".center
(30,'='))
import json
str_v1
= json
.dumps
(dic_v1
)
print("字典转换为字符串后的值:",str_v1
)
print("使用dumps转换字典为字符串类型:",type(str_v1
))
print("#将字符串转换为字典".center
(30,'='))
dic_v2
= json
.loads
(str_v1
)
print("字符串转换为字典后的值:",dic_v2
)
print("使用loads转换字符串为字典类型:",type(dic_v2
))
dic_v3
= {"确诊":1000,"治愈":800,"死亡":200}
for dic_key
in dic_v3
.keys
():
print(dic_key
)
for dic_value
in dic_v3
.values
():
print(dic_value
)
for dic_item
in dic_v3
.items
():
print(dic_item
)
print("访问字典中的某个元素",dic_v3
['治愈'])
print(dic_v3
.get
("治愈","没有您的访问内容"))
dic_v4
= {"学生1":{"姓名":"沃登","年龄":21,"性别":"男"},
"学生2":{"姓名":"沃尔登","年龄":22,"性别":"男"}}
dic_v5
= [{"姓名":"沃登","年龄":21,"性别":"男"},
{"姓名":"沃尔登","年龄":22,"性别":"男"}]
import pandas
as pd
stu_df
= pd
.DataFrame
(dic_v4
)
stu_df
.to_csv
("student.csv")
pd
.DataFrame
(dic_v4
)
dic_v6
= {'confirm': 61,'heal': 195,'dead': 4}
dic_v7
= {'confirm': "确诊",'heal': "治愈",'dead':"死亡"}
dic_v8
= {}
for dic_v6_key
in dic_v6
.keys
():
dic_v8
[dic_v7
[dic_v6_key
]] = dic_v6
[dic_v6_key
]
dic_v8
name
= '沃尔德'
age
= 20
tel
= '12345678910'
print('我叫%s,年龄%d,电话是%s'%(name
,age
,tel
))
print('我叫{},年龄{},电话是{}'.format(name
,age
,tel
))
print('我叫{0},年龄{1},电话是{2}'.format(name
,age
,tel
))
import pandas
as pd
file_name1
= open('美国_country_real_data.csv',encoding
= 'utf-8')
file_v1
= pd
.read_csv
(file_name1
)
file_v2
= file_v1
.drop
('Unnamed: 0',axis
=1)
file_v2
[['名称','总计确诊']]
file_v2
.loc
[:,['名称','总计确诊','总计治愈']]
file_v2
.loc
[file_v2
['总计治愈']>50000,['名称','总计确诊','总计治愈']]
import pandas
as pd
file_name1
= open('china_hist_data.csv',encoding
= 'utf-8')
file_v1
= pd
.read_csv
(file_name1
)
file_v2
= file_v1
.drop
('Unnamed: 0',axis
=1)
file_v2
['时间']=pd
.to_datetime
(file_v2
['时间'])
file_v2
.dtypes
file_v4
= file_v2
.iloc
[:,[1,2,3,11,12,9,15]]
file_v5
= file_v4
.set_index
('时间')
file_v5
file_v5
.index
= file_v5
.index
.month
file_v5
.loc
[:,['当天疑似','当天确诊']].groupby
(file_v5
.index
).sum()
file_v6
= file_v4
.set_index
('时间')
file_v6
.index
= file_v6
.index
.day
file_v6
file_v6
.loc
[:,['当天疑似','当天确诊']].groupby
(file_v6
.index
).agg
(['sum','max','min'])
import matplotlib
.pyplot
as plt
plt
.rcParams
['font.sans-serif'] = 'SimHei'
plt
.rcParams
['axes.unicode_minus'] = False
x
= ['治愈','死亡','确诊']
y
= [100,150,300]
y1
= [500,200,800]
fig
= plt
.figure
(figsize
=(8,6),dpi
=80)
plt
.title
('疫情数据')
plt
.xlabel
('类型')
plt
.ylabel
('人数')
plt
.ylim
(0,1000)
plt
.yticks
(range(100,1000,100))
plt
.legend
(['2月12日','2月13日'])
plt
.plot
(x
,y
,'r--',
x
,y1
,'b:')
for i
in range(len(x
)):
plt
.annotate
(xy
= (i
,y
[i
]+5),s
=y
[i
])
plt
.annotate
(xy
= (i
,y1
[i
]+5),s
=y1
[i
])
plt
.show
()
import pandas
as pd
city_code
= {'Beijing':'010','Tianjian':'022','Guangzhou':'020','Dalian':'0411','Shanghai':'021','Changchun':'0431'}
city_se
= pd
.Series
(city_code
)
city_se
转载请注明原文地址:https://tech.qufami.com/read-18727.html