[英]Python panda filtering on
我正在嘗試過濾文件,以便保留“單元格名稱”為LM-MP-NW-URBAN-GROCERY S & SPAZA
數據行,但是當我運行上述代碼時,我得到
“語法錯誤:無效語法”
我使用帶有數字的列嘗試了該代碼,並且它使用==
和<, >
符號。
另外,請幫助我如何過濾多個項目 - 例如在上面的代碼中,我想保留所有單元格名稱為LM-MP-NW-URBAN-GROCERY S & SPAZA
或KZN-GP-EC-URBAN_GROCERY S & SPAZA
先感謝您
#Import libraries
import pandas as pd
import os
import glob
#Set working directory and create list of raw files
os.chdir(r'C:\Users\Shab7002\Documents\data science\18 10 9\nestle 708294\infant')
lorf = glob.glob('*.txt')
#Create empty dataframe and concatenate raw files
df_mrgd = pd.DataFrame()
for file in lorf:
df_add = pd.read_csv(file,sep='\t', encoding='latin-1')
df_mrgd = pd.concat([df_mrgd, df_add.head(10)])
#Filter columns
filt_col = ['PeriodVFP', 'Product name', 'MBD Name', 'Outlet name', 'Cell Name', 'Sales', 'SalesValue', 'SalesVolume']
#filter rows
df_filtered = df_mrgd[filt_col].query('Cell Name== "LM-MP-NW-URBAN-GROCERY S & SPAZA"')
#and export concatenated data frame
df_filtered.to_excel('mu.xlsx')
這是根據 Pandas 中的多個Cell Name
值過濾行的方式:
df_filtered = df_mrgd.loc[df_mrgd['Cell Name'].isin(["LM-MP-NW-URBAN-GROCERY S & SPAZA", "KZN-GP-EC-URBAN_GROCERY S & SPAZA"]), filt_col]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.