python零碎整理

.python新学代码。

1. 循环创建列表

exec('l%s = []' % item)##

2.#传入x和n两个列表,将n列表中0的数值删去,并把对应位置上x的值也删掉。

def del_0(x,n):

    l_0 = []

    for i in np.arange(len(x)):

        if n[i] == 0.0:

            l_0.append(i)

    x = [x[i] for i in range(0,len(x),1) if i not in l_0]

    n = [n[i] for i in range(0,len(n),1) if i not in l_0]

    return x,n

3.平滑l列表,k为滑动窗口大小,输出平滑后l列表。

def smooth_l(l,k):

    smooth_l = []

    for i in np.arange(len(l) - k + 1):

        smooth_l.append(sum(l[i:i+k])/k)

return smooth_l

4.循环函数时候,l1,l2…等是不同的列表,输入参数会变l1,l2,l3…,

 window_len = 5

 for i in [1,3,4,5,6]:

       #错误:exec('xxx%s = smooth_l('l' + str(i),window_len)' %i),因为此时不会把'l' + str(i)认

#识成变量,所以需要eval函数来将字符串变成变量

    xx = eval('x' + str(i))#eval函数作用是将字符串变成变量(列表、元组、字典)

    ll = eval('l' + str(i))

    exec('xxx%s = smooth_l(xx,window_len)' %i)

exec('lll%s = smooth_l(ll,window_len)' %i)

5.python中字典转化为dataFrame(先将dict转为series,再转dataFrame):

pd.DataFrame(pd.Series(partition),columns=['cato'])
6.通过字典的值找键(返回该值的第一个键,值为i)

list(d.keys())[list(d.values()).index(i)]

7. 对注释进行字体大小和颜色设置

<font color=blue size=5 face=黑体>三.对这15类vc的投资数量进行画图看看</font>

8.显示中文字体。

import matplotlib

print(matplotlib.matplotlib_fname())

from matplotlib.font_manager import _rebuild

_rebuild()

matplotlib.rcParams['font.sans-serif']=['SimHei']

matplotlib.rcParams['axes.unicode_minus']=False

# matplotlib.rcParams['font.family']='sans-serif'

import matplotlib.pyplot as plt

plt.plot([1,2,3,4],[2,3,4,5],label = '中文')

plt.legend()

9.将列表中每个数字进行处理,都减去该列表最小值/(最大值 – 最小值)等。

list(map(lambda x: (x - min(xxx1))/(max(xxx1) - min(xxx1)), xxx1))

10.查看当前路径

import os

print(os.path.abspath('.'))

11. d是一个字典,通过字典的值来得到该值的键。

list (d.keys()) [list (d.values()).index (k)]

12.networkx求k-派系

list(k_clique_communities(G,5))

13. frozenset(冻结集,不能增改)转变为list

map(list, sets) 第二个为frozenset

14. 在python列表中找到最长的列表(二维列表找到最长的内部列表)

max(list,key = len)

15. python提取字符串中的汉字数字字母

#提取汉字

import re

string = "hello,world!!%[545]你好234asd完全额。。。"

str = re.sub("[A-Za-z0-9\!\%\[\]\,\。]", "", string)

print(str)

#从字符串中提取数字

totalCount = '100abcdef'

totalCount = re.sub("\D", "", totalCount)

print(totalCount)

#提取字母字符串

import re

string = "hello,world!!%[545]爱迪生234世界。。。"

result = ''.join(re.findall(r'[A-Za-z]', string))

print(result)

16.将一个列表中元素两两配对

import itertools

l = [1,2,3,8]

list(itertools.combinations(l, 2))

17.python中表示幂次方:**

10^2表示为10**2

18. def combine(l, n): ##列表内几个元素的组合,不考虑顺序

    answers = []

    one = [0] * n

    def next_c(li = 0, ni = 0):

        if ni == n:

            answers.append(copy.copy(one))

            return

        for lj in np.arange(li, len(l)):

            one[ni] = l[lj]

            next_c(lj + 1, ni + 1)

    next_c()

return answers

19. ##得到数量级,输入0.0001,输出-4

def magnitude (value):

    if (value == 0): return 0

    return int(math.floor(math.log10(abs(value))))

20.dataframe筛选包含指定字符的列名:

[x for x in df.columns if '指定字符' in x]

dataframe筛选包含指定字符的行名:

df[df['列名'].str.contains('指定字符')]
21.pandas根据某列进行排序:pd.sort_values("xxx",inplace=True)

22.python中help功能:help(pd.DataFrame.drop_duplicates)

                                        help(print)

23. python画图matplotlib中设置图横纵坐标为整数。

from matplotlib.ticker import MaxNLocator

plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True)) plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True))

24.networkx删除一个图的孤立节点。

G.remove_nodes_from(list(nx.isolates(G)))

25.对于图G(python中的非基础类型)的深复制:

import copy

G21 = copy.deepcopy(G11)

这样改变G11,G21就不会改变。

26.对于读取csv文件后出现制表符“\t”。

pd.read_csv("风险投资文件/vcinfo.csv",sep='\t')

27. 读取csv文件时候出现错误Error tokenizing data. C error: Expected 3 fields in line 5, saw 6
data_4 =pd.read_csv("风险投资文件/tempinfo.csv",sep = '\t')
 

28.python找出dataframe某列不为nan的。

df[df['Column'].notna()]

29.对列表l进行排序:

sorted(l)

30.删除列表l指定值。

for i in range(len(l)-1,-1,-1):

        if l[i]==[]:

            l.remove([])

31.多层嵌套列表变成单层。

list(itertools.chain.from_iterable(l))

32.字符串转成日期:

exchange_rate['日期'] = exchange_rate['日期'].apply(lambda x :datetime.strptime(x,'%Y-%m-%d'))

将日期后的时间去掉:

data_1['成立日期'] = data_1['成立日期'].apply(lambda x : x.strftime("%Y/%m/%d"))

33.字典子集(键的子集)

a = {'a':1,'b':2,'c':3}

lista = ['a','b']

x = dict((key,value) for key,value in a.items() if key in lista)

print(x)

34.不显示中文

import matplotlib

matplotlib.rcParams['font.sans-serif'] = ['SimHei']

matplotlib.rcParams['font.family'] ='sans-serif'

matplotlib.rcParams['axes.unicode_minus'] = False

34. plt.plot(list(dic.keys()),list(dic.values()),label = cat_mat[i],linestyle='--',marker = '+')

35.pandas中max(df[‘列名’]),该列出现空字符串,是会出错,但是如果有np.nan不影响

36.范数

37.python中strip函数

str.strip([chars]);

chars -- 移除字符串头尾指定的字符序列。

返回移除字符串头尾指定的字符生成的新字符串。

38.误删jupyter notebook中文件时:

运行      cd ~/.local/share/Trash/files/      即输出为误删文件路径

39. 设置markdown格式

*倾斜*`

**加粗**

***斜体加粗***

~~删除线~~

**<font color = blue size = 3>题目。。。。。 </font>**

40.双对数时候负数显示乱码

 plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']  # 用来正常显示中文标签

plt.rcParams['axes.unicode_minus'] = False
 

41. 关于jupyter的故障重启

(这个重启和下面图直接断开不一样,直接断开不能杀死服务)

第一步:查看正在启动的进程服务:

ps -ef |grep jupyter-notebook

第二步:杀某进程(55686是号码,通过上面来看):

kill 55686

第三步:再查看正在启动的进程服务,看是否杀死

ps -ef |grep jupyter-notebook

第四步:再重启jupyter

nohup jupyter-notebook &

 42.jupyter安装插件

(安装后要记得重启jupyter服务)

43.循环读取文件,并以文件名给变量命名

path = os.getcwd()+'/风险投资文件/random_vc/random_vc真实分布数据'

files = os.listdir(path)

files_xls = [f for f in files if f[-4:] == 'xlsx' and f != 'random_vc.xlsx' and f!='random_su.xlsx']

for filename in files_xls:

    pathf = path + '/' + filename

    df = pd.read_excel(pathf,index_col=0)

#     test = pd.read_excel(pathf,index_col = 0) #可以运行

    exec(f'{filename[:-5]} = df')#这里的-5是要去掉'.xlsx'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值