2020-07-29

python算法
1、求三位数组合
四个数字能组成多少个互不相同且无重复数字的三位数?

lst = [3,6,2,7]
a = []
for i in lst:
  for j in lst:
    for k in lst:
      if i != j and i != k and j != k:
        a.append('{first} {secend} {third}' . format(first = i ,secend = j,third = k))
print('共有{count}个符合条件的数字'.format(count(len(a))))

2.生成矩阵
已知两个矩阵
lst_1 = [1,2,3,4]
lst_2 = ['a' , 'b' , 'c' ,'d']

a = []
for i in lst_1:
    b = []
    for j in lst_2:
        value = str(i) + j
        b.append(value)
    a.append(b)

print(a)  

3、列表偏移
已知列表lst = [1,2,3,4,5]
要求列表向右偏移两位后变成 lst = [4,5,1,2,3]

lst = [1, 2, 3, 4, 5]

count = int(input('输入偏移位数:'))
count %= len(lst)
print(count)

for i in range(count):
    tmp = lst[-1]
    for j in range(len(lst) - 1, 0, -1):
        lst[j] = lst[j - 1]
    lst[0] = tmp
print(lst)

pandas 基础
1、请利用多种方式转化为DataFrame,

1.
data = {'python': ['Pandas','numpy','flask','django',np.nan,'ElasticSearch','Pandas','pymysql'],
       'score': ['1.0','2.0',np.nan,'4.0','5.0','6.0','7.0','10.0']}

df = pd.DataFrame(data)
df

2.
python = pd.Series(['Pandas','numpy','flask','django',np.nan,'ElasticSearch','Pandas','pymysql'],name= 'python')
python = python.to_frame()
score = pd.Series([1,2,np.nan,4,5,6,7,10],name = 'score')
score = score.to_frame()
data = pd.merge( python ,score , left_index= True, right_index=True)
data

2、提取含有字符串‘pandas’的行

df[df['python']== 'Pandas']

3、输出df的所有列名

df.columns

4、修改第二列列名为‘popularity’

df.rename(columns = {'score' : 'popularity'},inplace = True)
df

5、统计python列中每种模块包出现的次数

df['python'].value_counts()

6、将空值用上下值的平均值填充

df['popularity'] = df['popularity'].fillna(df['popularity'].interpolate())
df

你可能感兴趣的:(2020-07-29)