Python数据类型处理

空值:

判断空值:

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值