python输出字符,主要为结合变量形成新的变量名
year = 2016
event = 'Referendum'
f'Results of the {year} {event}''Results of the 2016 Referendum'
yes_votes = 42_572_654
no_votes = 43_132_495
percentage = yes_votes / (yes_votes + no_votes)
'{:-9} YES votes {:2.2%}'.format(yes_votes, percentage)
' 42572654 YES votes 49.67%'
print('The value of pi is approximately %5.3f.' % math.pi)The value of pi is approximately 3.142.
创建表
#构建一个表
df = pd.DataFrame(np.arange(12).reshape(3, 4),columns=['A', 'B', 'C', 'D'])
df
删除列,需要注明axis=1或者是columns=xxx
#一种表达
df.drop(['B', 'C'], axis=1)#另一种表达
df.drop(columns=['B', 'C'])#还可以这样表达
df.drop(labels=['B', 'C'], axis=1)
删除行
df.drop([0, 1])
复合索引表的删除行列
#新建一个复合索引的表
midx = pd.MultiIndex(levels=[['lama', 'cow', 'falcon'],['speed', 'weight', 'length']],codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],[0, 1, 2, 0, 1, 2, 0, 1, 2]])
df = pd.DataFrame(index=midx, columns=['big', 'small'],data=[[45, 30], [200, 100], [1.5, 1], [30, 20],[250, 150], [1.5, 0.8], [320, 250],[1, 0.8], [0.3, 0.2]])
df
删除行
df.drop(index='length', level=1)
删除列
df.drop(index='cow', columns='small')