pythoncsv篩選
① 使用python,如何遍歷csv文件的每一行記錄的每一個欄位值並進行欄位值是否符合條件的判斷
按行讀進來,用「,」分割,然後把符合條件的保留,最終把保留的寫到新文件中。注意讀進來的都是字元,所以判斷數字的時候要做一次轉換。又由於不是全部是數字,所以這里用一些try之類的或者預先判斷一下是否全部是數字。實現起來不難。估計不超過10多行代碼。
② 如何使用python對csv/表格進行篩選和計算
python2.5之後就自帶sqlite3了,這種事情還是資料庫在行~
③ python 查找csv數據的問題
你好
這個比較簡單吧!
importcsv
withopen('d:\1.csv','rb')ascsvfile:
reader=csv.reader(csvfile)
column=[row[3]forrowinreader]
print"第一條數據是:"+column[1]
print"最後條數據是:"+column[-1]
print("最後一條數據位置{:^5}".format(len(column)-1))
望採納!
④ 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("ring_time(number)=0的個數:",newdata[newdata['ring_time(number)'] ==0].count()['ring_time(number)'])
print("ring_time(number)=1,2,3的個數:",newdata[(newdata['ring_time(number)'] >0) & (newdata['ring_time(number)'] <4)].count()['ring_time(number)'])
print(newdata[newdata['ring_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['ring_time']=pd.to_datetime(newdata['submission_audit_time'])-pd.to_datetime(newdata['contact_time'])
newdata['ring_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['ring_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[['購葯日期', '星期','社保卡號','商品編碼', '商品名稱', '銷售數量', '應收金額', '實收金額' ]]