20200729-数据蛙-第九期-2
2020-08-07 本文已影响0人
大板锹
pandas基础
1、请利用多种方式将截图内容转成DataFrame。
image.png导入包:
import pandas as pd
import numpy as np
function1:
pd.DataFrame(data=[['Pandas',1.0],
['numpy',2.0],
['flask',np.NaN],
['django',4.0],
[np.NaN,5.0],
['EasticSearch',6.0],
['Pandas',7.0],
['pymysql',10.0]],
columns = ['python','score'],
)
function2:
python_dict = { 'python':['Pandas','numpy','flask','django','NaN','EasticSearch','Pandas','pymysql'],
'score': [1.0, 2.0, np.NaN, 4.0, 5.0,6.0,7.0,10.0]
}
df = pd.DataFrame(python_dict)
df
2、提取含有字符串“Pandas”的行
df[df['python'] == 'Pandas']
image.png
3、输出df的所有列名
print(df.columns)
Index(['python', 'score'], dtype='object')
4、修改第二列列名为‘popularity’
df.rename(columns={'score':'popularity'},inplace=True)
df
image.png
5、统计python列中每种模块包出现的次数
df['python'].value_counts()
image.png
6、将空值用上下值的平均值填充
df['popularity'] = df['popularity'].fillna(df['popularity'].interpolate())
df
image.png
image.png
python算法
1、求三位数组合
lst = [3, 6, 2, 7]
这四个数字能组成多少个互不相同且无重复数字的三位数?比如362算一个,326算一个,请逐个输出他们
lst = [3, 6, 2, 7]
lst2 = []
for i in lst:
for j in lst:
for k in lst:
if i != j and i != k and j != k:
result = i*100+j*10+k
lst2.append(result)
print(lst2)
image.png
2、生成矩阵
已知两个列表
lst_1 = [1, 2, 3, 4]
lst_2 = ['a', 'b', 'c', 'd']
请写算法,将两个列表交叉相乘,生成如下的矩阵
[['1a', '2a', '3a', '4a'],
['1b', '2b', '3b', '4b'],
['1c', '2c', '3c', '4c'],
['1d', '2d', '3d', '4d']]
lst_1 = [1, 2, 3, 4]
lst_2 = ['a', 'b', 'c', 'd']
lst_4 = []
for i in lst_2:
lst_3 = []
for j in lst_1:
lst_3.append(str(j) + i)
lst_4.append(lst_3)
print(lst_4)
3、列表偏移
已知列表
lst = [1,2,3,4,5]
要求列表向右偏移两位后,变成
lst = [4,5,1,2,3]
lst = [1,2,3,4,5]
result = lst[len(lst)-2:]+lst[:len(lst)-2]
print(result)