pandas
numpy
dbfread
df = pd.DataFrame(pd.read_excel('filepath'))
#导入模块
from dbfread import DBF
#数据表文件名
table = DBF('test.dbf')
#遍历数据表中(没加删除标志)的记录
for record in table:
for field in record:
print(field, "=", record[field], end = ",")
print()
print("*" * 40)
#遍历数据表中(加了删除标志)的记录
for record in table.deleted:
for field in record:
print(field, "=", record[field], end = ",")
print()
df.dropna(axis='0', subset='columname',how='any')
axis='0’表示删除行,‘1’为列
添加subset可选择某一列判断是否有空值
how=‘any’ 表示只要有一个空值就删除
'all’表示全为空才删除
用于标记series中的值、dataframe中的记录行是否重复,重复未True,不重复为False
pandas.DataFrame.duplicated(self, subset=None, keep='first')
pandas.Series.duplicated(self, keep='first')
subset:用于识别重复的列标签或列标签序列,默认所有列标签
keep=‘frist’:除了第一次出现外,其余相同的被标记为重复
keep=‘last’:除了最后一次出现外,其余相同的被标记为重复
keep=False:所有相同的都被标记为重复的
1 | 2 | 3 | 4 |
---|---|---|---|
one | 1 | AA | a |
one | 2 | BB | a |
two | 1 | CC | b |
two | 2 | DD | c |
two | 1 | EE | b |
three | 1 | FF | a |
four | 1 | GG | c |
import numpy as np
import pandas as pd
#标记DataFrame重复例子
df = pd.DataFrame({'col1': ['one', 'one', 'two', 'two', 'two', 'three', 'four'], 'col2': [1, 2, 1, 2, 1, 1, 1],
'col3':['AA','BB','CC','DD','EE','FF','GG']},index=['a', 'a', 'b', 'c', 'b', 'a','c'])
#duplicated(self, subset=None, keep='first')
#根据列名标记
#keep='first'
df.duplicated()#默认所有列,无重复记录
df.duplicated('col1')#第二、四、五行被标记为重复
df.duplicated(['col1','col2'])#第五行被标记为重复
#keep='last'
df.duplicated('col1','last')#第一、三、四行被标记重复
df.duplicated(['col1','col2'],keep='last')#第三行被标记为重复
#keep=False
df.duplicated('col1',False)#Series([True,True,True,True,True,False,False],index=['a','a','b','c','b','a','c'])
df.duplicated(['col1','col2'],keep=False)#在col1和col2列上出现相同的,都被标记为重复
type(df.duplicated(['col1','col2'],keep=False))#pandas.core.series.Series
#根据索引标记
df.index.duplicated()#默认keep='first',第二、五、七行被标记为重复
df.index.duplicated(keep='last')#第一、二、三、四被标记为重复
df[df.index.duplicated()]#获取重复记录行
df[~df.index.duplicated('last')]#获取不重复记录行
#标记Series重复例子
#duplicated(self, keep='first')
s = pd.Series(['one', 'one', 'two', 'two', 'two', 'three', 'four'] ,index= ['a', 'a', 'b', 'c', 'b', 'a','c'],name='sname')
s.duplicated()
s.duplicated('last')
s.duplicated(False)
#根据索引标记
s.index.duplicated()
s.index.duplicated('last')
s.index.duplicated(False)
DataFrame.drop_duplicates(subset=None, keep='first', inplace=False)
df.drop_duplicates('B','first',inplace=True)
list1=['a','a','b','c']
list2=list(set(list1))
#如果想保留排序
list1=['a','a','b','c']
list2=sorted(set(list1),key=list1.index)
#或者:
list2=list(set(list1))
list2.sort(key=list1.index)
df.to_excel('filename', sheet_name='Sheetname')