1 获取某一列为空的所有行数据
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!**获取某一列为空的所有行数据**
df[np.isnan(df['列名'])]
2两个dataframe相减求差集
①dataframe1=dataframe1.append(dataframe2).drop_duplicates(keep=False)
②先把需要剔除的df的ID自动用tolist输出成一个列表
然后前一个df用~isin(列表)来剔除
code_list=df1['ID'].tolist()
df2=df2[~df2['ID'].isin(code_list)]
③list=df1['ID'].tolist()
df2=df2[df2['ID'].isin(list)]
df=pd.DataFrame({
'Name':['Alen','Bob','Cidy','Daniel','Ellen','Frankie','Gate','Hebe'],
'Gender':['Male','Male','Female','Male','Female','Male','Male','Female'],
'age':[18,19,18,20,17,21,20,22],
'Score':[80,90,93,87,96,100,88,98]})
对Gender列聚合然后查找列值符合female的datafram
df_group=df.group('Gender').get_group('Female')
对Gender和age列聚合然后查找列值符合female 且17岁的dataframe
gr=df.groupby(['Gender','age'])
da=gr.get_group(('Female',17))
da.values()可以取数组值
df.stack(level=0) 把第0层列索引变成行索引
df.stack(level=1) 把第1层列索引变成行索引
df.unstack(level=0) 把1级行索引转变为列索引 透视表
df.unstack(level=1) 把2级行索引转变为列索引 透视表
from sqlalchemy import create_engine
# conn = create_engine('mysql+pymysql://hal:123456@localhost:3306/nz1904?charset=utf8')
conn = create_engine('mysql+pymysql://prod_adb_rw:58r7nxTuu7dDdkz2@am-uf6laf8yh27660jd4131930.ads.aliyuncs.com:3306/db_personas?charset=utf8')
import pandas as pd
chinatotal = pd.read_sql('select * from user_org_sku_v ',conn)
chinatotal
chinatotal['消费金额'].fillna(chinatotal['消费金额'].mean(), inplace=True)
# 删除全空的行
chinatotal.dropna(how='all',inplace=True)
chinatotal.drop_duplicates(inplace=True)
3.pandas实现sql的group_concat()
df.groupby(‘team’).apply(lambda x: ‘,’.join(x.user))
df.groupby(‘team’).apply(lambda x: list(x.user))
4去掉空格换行制表符
aStr = 'hello wel\rcome \t to \t zy \n oYeah'
print(''.join(aStr.split()))
首先奖原字符串进行分割,然后组合
split() --> ['hello', 'wel', 'come', 'to', 'zy', 'oYeah']
join() --> hellowelcometozyoYeah
5datafram行列互相转换
https://www.cnblogs.com/nxf-rabbit75/p/11088925.html