info方法查看数据类型
info()
方法可以用来查看数据的类型以及不为null的行数
import pandas as pd
df = pd.read_excel(r"C:\Users\matrix\Desktop\jd-ads-discovery_2022-09-08_2022-09-08.xls")
df.info()
dtypes查看某列的数据类型
import pandas as pd
df = pd.read_excel(r"C:\Users\matrix\Desktop\jd-ads-discovery_2022-09-08_2022-09-08.xls")
for column in df.columns:
print(df[column].dtypes)
输出符合指定类型的列
import pandas as pd
df = pd.read_excel(r"C:\Users\matrix\Desktop\lazada-ads-discovery_2022-09-08_2022-09-08.xls")
for column in df.columns:
if df[column].dtypes == 'int64':
print(column)
查看空值的列
import pandas as pd
df = pd.read_excel(r"C:\Users\matrix\Desktop\lazada-ads-discovery_2022-09-08_2022-09-08.xls")
# 输出包含空值的列
df.loc[:,(df.isnull()).any()].columns
# 输出全为空值的列
df.loc[:,(df.isnull()).all()].columns
求某列的sum和
df[column].sum()
【注】包含非数字字符的列会按照字符串拼接处理
import pandas as pd
df = pd.read_excel(r"C:\Users\matrix\Desktop\lazada-ads-discovery_2022-09-08_2022-09-08.xls")
for column in df.columns:
dtype = df[column].dtype
if dtype == 'int64' or dtype == 'float64':
print(df[column].sum())
python正则匹配
import re
str= '0.00%'
## 0.00%
print(re.search(r'0+\.*0*%$', str).group())
str= '0.00%'
## 0.00%
print(re.fullmatch(r'(0+\.*0*%$)', str) != None)
某列匹配正则
# array([ True, True, True, False])
df['Direct CVR'].apply(lambda s: re.fullmatch(r'0+\.*0*%$', s) != None).values
判断是否全为空或者全为0
import pandas as pd
import re
from pandas import Series,DataFrame
import numpy as np
df = pd.read_excel(r"C:\Users\Sun Tian Sheng\Desktop\lazada-ads-discovery_2022-09-08_2022-09-081.xls")
flag = False
for column in df.columns:
if dtype == 'int64' or dtype == 'float64':
sum = df[column].sum()
if sum != 0:
flag = True
else:
if df[column].isnull().all() == False:
flag = True
f = lambda s: re.fullmatch(r'0+\.*0*%$', s) != None
blank_value_rows = np.sum(df[column].apply(f).values == True)
column_rows = len(df[column])
if blank_value_rows != column_rows:
flag = True
print(flag)
参考
【1】python 利用all(),any() 查找存在0、全是0、含缺失值的列
【2】python pandas对多列求和
【3】pandas中查看数据类型的几种方式 (含python、pandas、numpy类型对照表)
【4】Python正则表达式,这一篇就够了!
【5】pandas(12):数据清洗(缺失值)
【6】pandas.DataFrame.apply