空值:
判断空值:
import pandas as pd
pd.isnull(x)
df 列去掉空值:
df.dropna(subset=[‘label’])
df[df.label.notnull()]
判断数据类型:
数字(int)、浮点(float)、字符串(str),列表(list)、元组(tuple)、字典(dict)、集合(set)
#判断变量类型的函数 def typeof(variate):
type=None if isinstance(variate,int):
type = “int”
elif isinstance(variate,str):
type = “str”
elif isinstance(variate,float):
type = “float”
elif isinstance(variate,list):
type = “list”
elif isinstance(variate,tuple):
type = “tuple”
elif isinstance(variate,dict):
type = “dict”
elif isinstance(variate,set):
type = “set”
return type
python 中的循环语法
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_list = [i**2 for i in numbers if i %2 ==0 ]
even_list
在不同条件下的comprehension
form1
def some_function(k):
# some lines of code doing something
# ...
another_list = []
for i in range(k):
if i%2 ==0:
another_list.append( (i*7 + 5) % 13 )
else:
another_list.append( (i*5 + 7) % 23 )
# ...
# more lines of code doing something else
form2
another_list = [(i*7 + 5) % 13 if i%2==0 else (i*5 + 7) % 23 for i in range(k) ]
实现快捷语句
single_string = ', '.join( [str(k) for k in range(100)] )
single_string
output:
sentence split
sentence = input("How are you doing : ")
words = sentence.split()
words.sort()
print("Here are the sorted words :")
for word in words:
print(word)
O/P:
How are you doing : I am doing very good
Here are the sorted words :
I
am
doing
good
very