Some lessons learned about the recent use of pandas

1.pandas de-emphasis function drop_duplicates

DataFrame.drop_duplicates(subset=None, keep='first', inplace=False)

Wherein the parameter is used to specify the subset going heavy columns, by default all columns;

keep parameters are first, last, False three options, first expressed reservations first term duplicates in, last to retain the last one, delete all False, the default is first;

inplace parameter takes bool value to True represents a direct modification in the original variable to False means creating a copy of the save the changes, the default is True.

1 例:
2 data = [[1,2,3],[3,2,1],[1,1,3],[1,3,2]]
3 df = pd.DataFrame(data, columns=('a','b','c'))

# The column to a heavy 
df.drop_duplicates ( ' a ' )

 

 2. When a listed as a string, you want to delete the row that contains the column of a string

= Data [[. 1, ' Blue Cheung excavator ' ], [2, ' New Oriental pot ' ], [3, ' Blue Cheung Ziglar ' ], [4, ' New Oriental English ' ]] 
DF = pd.DataFrame ( Data, Columns = ( ' ID ' , ' product ' ))

# OK Delete "item" column with "Blue Xiang" in 
DF DF = [~ DF [ ' product ' ] .str.contains ( ' Lanxiang ' )]

 

 3. The two-dimensional list into a one-dimensional, such as [[1,2,3], [4,5,6], [7,8,9]] into [1,2,3,4,5, 6,7,8,9] - encountered during the processing of data

Use of chain modules itertools

from itertools import chain
a = [[1,2,3],[4,5,6],[7,8,9]]
print('a:',a)
b = list(chain.from_iterable(a))
print('b:',b)

 

Guess you like

Origin www.cnblogs.com/fxm1/p/11539811.html