當前位置:成語大全網 - 新華字典 - Python數據處理:篩選、統計、連表、拼接、拆分、缺失值處理

Python數據處理:篩選、統計、連表、拼接、拆分、缺失值處理

file1_path ='E:/Users/lenovo/Desktop/中視/622召回.csv' # 源數據

格式:file1=pd.read_csv(file1_path)

pd.read_csv(file1_path,encoding='gbk')

pd.read_csv(file1_path,encoding='gbk',skiprows=[2,3])

pd.read_csv(file1_path,encoding='gbk',skiprows=lambda x:x%2==1)

pd.read_csv(file1_path,encoding='gbk',keep_default_na=False)

new=pd.DataFrame()

new.new[[0,1,2]]

new.new[0:2]

查詢結果同上

new.loc[new['激活數']>1000]

loc和iloc的區別:

loc:純標簽篩選

iloc:純數字篩選

#篩選出new的某兩列

new=new.loc[:,['phone','收件人姓名']]

#篩選new的第0,1列

new.iloc[:,[0,1]]

使用‘==’篩選-篩查“崔旭”的人(只能篩查指定明確的)

#new=file1.loc[(file1['收件人姓名']=='崔旭')|(file1['收件人姓名']=='崔霞')]

#print(new)

#使用loc函數篩選-str.contains函數-篩查名字中包含'亮'和'海'的人

#new=file1.loc[file1['收件人姓名'].str.contains('亮|海')]

#print(new)

#使用loc函數篩選-str.contains函數-篩查'崔'姓的人

#new=file1.loc[file1['收件人姓名'].str.startswitch('崔')]

#print(new)

df = df[(df['DEPOSIT_PAY_TIME_x'] .notnull() ) & (df['DEPOSIT_PAY_TIME_x'] != "" )]

print("during_time(number)=0的個數:",newdata[newdata['during_time(number)'] ==0].count()['during_time(number)'])

print("during_time(number)=1,2,3的個數:",newdata[(newdata['during_time(number)'] >0) & (newdata['during_time(number)'] <4)].count()['during_time(number)'])

print(newdata[newdata['during_time(number)'] ==0])

newdata[newdata['Team']. isin (['England','Italy','Russia'])][['Team','Shooting Accuracy']]

df.年齡.value_counts()

1.修改指定位置數據的值(修改第0行,’創建訂單數‘列的值為3836)

new.loc[0,'創建訂單數']=3836

2.替換‘小明’-‘xiaoming’

df.replace({'name':{'小明':'xiaoming'}})

3.批量替換某壹列的值(把‘性別’列裏的男-male,女-felmale)

方法壹:df['性別']=df['性別'].map({'男':'male','女':'female'})

方法二:df['性別'].replace('female','女',inplace=True)

或df['性別']=df['性別'].replace('female','女') 這就是inplace的作用

+df['性別'].replace('male','男',inplace=True)

4.替換列索引

df.columns=['sex','name','height','age']

或者:df.rename(columns={'性別':'sex','姓名':'name','身高':'height','年齡':'age'})

5.刪除某壹列

del df['player']

6. 刪除某壹列(方法二),刪除某壹行(默認axis=0刪除行,為1則刪除列)

刪除某壹列(方法二)

df.drop('性別',axis=1)

刪除某壹行

df.drop(1,axis=0)

file1=pd.read_csv(file1_path)

file2=pd.read_csv(file2_path)

new1=pd.DataFrame()

new1['phone']=file1['phone']

new1['contact_time']=file1['contact_time']

new2=pd.DataFrame()

new2['phone']=file2['phone']

new2['submission_audit_time']=file2['提交審核時間']

newdata=pd.merge(new1,new2,on='phone',how='left')

df=pd.concat([df1,df2],axis=0)

4.2.2 橫向表連接

df=pd.concat([df1,df2],axis=1)

df1['地區'].str.split('·',3,expand=True)

df1:

df1[['城市', '城區','地址']] = df1['地區'].str.split('·', 3, expand = True)

5.1 缺失值刪除

data.dropna(axis=0,subset = ["Age", "Sex"]) ? # 丟棄‘Age’和‘Sex’這兩列中有缺失值的行

data.dropna(how = 'all') # 傳入這個參數後將只丟棄全為缺失值的那些行

data.dropna(axis = 1) ? # 丟棄有缺失值的列(壹般不會這麽做,這樣會刪掉壹個特征)

data.dropna(axis=1,how="all") ? # 丟棄全為缺失值的那些列

5.2 缺失值填充:pandas.DataFrame.fillna()函數

DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None, **kwargs)

功能:使用指定方法填充NA/NaN值

其中inplace=True就是直接在原有基礎上填滿

5.3 缺失值查詢:

缺失值數量查詢:df.isnull().sum()

缺失值行查詢:df[df.isnull().T.any()]

newdata['during_time']=pd.to_datetime(newdata['submission_audit_time'])-pd.to_datetime(newdata['contact_time'])

newdata['during_time(number)']=(pd.to_datetime(newdata['submission_audit_time'])-pd.to_datetime(newdata['contact_time'])).apply(lambda x: x.days)

new=pd.DataFrame()

new=newdata[newdata['during_time(number)'] ==0]

new.to_csv(save_path,encoding='utf-8-sig')

將數據按行拆分並存儲到不同的csv文件中:

path='C:/Users/EDZ/Desktop/工作/2021.08.19/'

for i in range(0,30):

df.loc[[i]].to_csv(path+str(i)+'.csv',encoding='gbk')

df = df[['購藥日期', '星期','社保卡號','商品編碼', '商品名稱', '銷售數量', '應收金額', '實收金額' ]]