Data wrangling python cheat sheet

Đọc viết. csv

# csv
df = pd.read_csv['file.csv', nrows = 5] 
pd.to_csv['file.csv']
# excel 
df = pd.read_excel['file.xlsx']
df = pd.read_excel[pd.ExcelFile['file.xlsx'], 'Sheet1']
pd.to_excel['file.xslx', sheet_name='Sheet1']

Metadata

df.rename[columns = {'col1': 'rename1', 
                     'col2': 'rename2'}]
df[['col1', 'col2', .. ]] # order cols
df.sort_values[[col1', 'col2'], 
               ascending = [True, False]]
0

df.rename[columns = {'col1': 'rename1', 
                     'col2': 'rename2'}]
df[['col1', 'col2', .. ]] # order cols
df.sort_values[[col1', 'col2'], 
               ascending = [True, False]]
1

df.rename[columns = {'col1': 'rename1', 
                     'col2': 'rename2'}]
df[['col1', 'col2', .. ]] # order cols
df.sort_values[[col1', 'col2'], 
               ascending = [True, False]]
2

df.rename[columns = {'col1': 'rename1', 
                     'col2': 'rename2'}]
df[['col1', 'col2', .. ]] # order cols
df.sort_values[[col1', 'col2'], 
               ascending = [True, False]]
3

df.rename[columns = {'col1': 'rename1', 
                     'col2': 'rename2'}]
df[['col1', 'col2', .. ]] # order cols
df.sort_values[[col1', 'col2'], 
               ascending = [True, False]]
4

df.rename[columns = {'col1': 'rename1', 
                     'col2': 'rename2'}]
df[['col1', 'col2', .. ]] # order cols
df.sort_values[[col1', 'col2'], 
               ascending = [True, False]]
5

Sắp xếp

df.rename[columns = {'col1': 'rename1', 
                     'col2': 'rename2'}]
df[['col1', 'col2', .. ]] # order cols
df.sort_values[[col1', 'col2'], 
               ascending = [True, False]]

Lọc

________số 8_______

Chức năng hữu ích

df.rename[columns = {'col1': 'rename1', 
                     'col2': 'rename2'}]
df[['col1', 'col2', .. ]] # order cols
df.sort_values[[col1', 'col2'], 
               ascending = [True, False]]
6

df.rename[columns = {'col1': 'rename1', 
                     'col2': 'rename2'}]
df[['col1', 'col2', .. ]] # order cols
df.sort_values[[col1', 'col2'], 
               ascending = [True, False]]
7 /
df.rename[columns = {'col1': 'rename1', 
                     'col2': 'rename2'}]
df[['col1', 'col2', .. ]] # order cols
df.sort_values[[col1', 'col2'], 
               ascending = [True, False]]
8

df.rename[columns = {'col1': 'rename1', 
                     'col2': 'rename2'}]
df[['col1', 'col2', .. ]] # order cols
df.sort_values[[col1', 'col2'], 
               ascending = [True, False]]
9

df[[df.col1 > 1000] | [df.col2 != "A"]]
df[collist] # collist = ['col1', ...]
df.iloc[0:5, :] / df.head[5] # by position
df.loc[[df.col1 > 5] & [df.col2 == "A"], 
       ['col1', 'col2']] # by label
df.drop[['col1', ...], axis = 1]
df.drop_duplicates[]
df.sample[frac=0.5 / n=10]
df.filter[regex = 'regex']
0

df[[df.col1 > 1000] | [df.col2 != "A"]]
df[collist] # collist = ['col1', ...]
df.iloc[0:5, :] / df.head[5] # by position
df.loc[[df.col1 > 5] & [df.col2 == "A"], 
       ['col1', 'col2']] # by label
df.drop[['col1', ...], axis = 1]
df.drop_duplicates[]
df.sample[frac=0.5 / n=10]
df.filter[regex = 'regex']
1

df[[df.col1 > 1000] | [df.col2 != "A"]]
df[collist] # collist = ['col1', ...]
df.iloc[0:5, :] / df.head[5] # by position
df.loc[[df.col1 > 5] & [df.col2 == "A"], 
       ['col1', 'col2']] # by label
df.drop[['col1', ...], axis = 1]
df.drop_duplicates[]
df.sample[frac=0.5 / n=10]
df.filter[regex = 'regex']
2 /
df[[df.col1 > 1000] | [df.col2 != "A"]]
df[collist] # collist = ['col1', ...]
df.iloc[0:5, :] / df.head[5] # by position
df.loc[[df.col1 > 5] & [df.col2 == "A"], 
       ['col1', 'col2']] # by label
df.drop[['col1', ...], axis = 1]
df.drop_duplicates[]
df.sample[frac=0.5 / n=10]
df.filter[regex = 'regex']
3

df[[df.col1 > 1000] | [df.col2 != "A"]]
df[collist] # collist = ['col1', ...]
df.iloc[0:5, :] / df.head[5] # by position
df.loc[[df.col1 > 5] & [df.col2 == "A"], 
       ['col1', 'col2']] # by label
df.drop[['col1', ...], axis = 1]
df.drop_duplicates[]
df.sample[frac=0.5 / n=10]
df.filter[regex = 'regex']
4

df[[df.col1 > 1000] | [df.col2 != "A"]]
df[collist] # collist = ['col1', ...]
df.iloc[0:5, :] / df.head[5] # by position
df.loc[[df.col1 > 5] & [df.col2 == "A"], 
       ['col1', 'col2']] # by label
df.drop[['col1', ...], axis = 1]
df.drop_duplicates[]
df.sample[frac=0.5 / n=10]
df.filter[regex = 'regex']
5 /
df[[df.col1 > 1000] | [df.col2 != "A"]]
df[collist] # collist = ['col1', ...]
df.iloc[0:5, :] / df.head[5] # by position
df.loc[[df.col1 > 5] & [df.col2 == "A"], 
       ['col1', 'col2']] # by label
df.drop[['col1', ...], axis = 1]
df.drop_duplicates[]
df.sample[frac=0.5 / n=10]
df.filter[regex = 'regex']
6

df[[df.col1 > 1000] | [df.col2 != "A"]]
df[collist] # collist = ['col1', ...]
df.iloc[0:5, :] / df.head[5] # by position
df.loc[[df.col1 > 5] & [df.col2 == "A"], 
       ['col1', 'col2']] # by label
df.drop[['col1', ...], axis = 1]
df.drop_duplicates[]
df.sample[frac=0.5 / n=10]
df.filter[regex = 'regex']
7

df[[df.col1 > 1000] | [df.col2 != "A"]]
df[collist] # collist = ['col1', ...]
df.iloc[0:5, :] / df.head[5] # by position
df.loc[[df.col1 > 5] & [df.col2 == "A"], 
       ['col1', 'col2']] # by label
df.drop[['col1', ...], axis = 1]
df.drop_duplicates[]
df.sample[frac=0.5 / n=10]
df.filter[regex = 'regex']
8

df[[df.col1 > 1000] | [df.col2 != "A"]]
df[collist] # collist = ['col1', ...]
df.iloc[0:5, :] / df.head[5] # by position
df.loc[[df.col1 > 5] & [df.col2 == "A"], 
       ['col1', 'col2']] # by label
df.drop[['col1', ...], axis = 1]
df.drop_duplicates[]
df.sample[frac=0.5 / n=10]
df.filter[regex = 'regex']
9

def function_name[var1, var2, ...]:
        for i in 1:10
                if line1:
                       do this
                elif line2:
                       do that
                else:
                       do none
        return[result]
0

def function_name[var1, var2, ...]:
        for i in 1:10
                if line1:
                       do this
                elif line2:
                       do that
                else:
                       do none
        return[result]
1

def function_name[var1, var2, ...]:
        for i in 1:10
                if line1:
                       do this
                elif line2:
                       do that
                else:
                       do none
        return[result]
2

def function_name[var1, var2, ...]:
        for i in 1:10
                if line1:
                       do this
                elif line2:
                       do that
                else:
                       do none
        return[result]
3

def function_name[var1, var2, ...]:
        for i in 1:10
                if line1:
                       do this
                elif line2:
                       do that
                else:
                       do none
        return[result]
4

def function_name[var1, var2, ...]:
        for i in 1:10
                if line1:
                       do this
                elif line2:
                       do that
                else:
                       do none
        return[result]
5

def function_name[var1, var2, ...]:
        for i in 1:10
                if line1:
                       do this
                elif line2:
                       do that
                else:
                       do none
        return[result]
6

def function_name[var1, var2, ...]:
        for i in 1:10
                if line1:
                       do this
                elif line2:
                       do that
                else:
                       do none
        return[result]
7

 

Viết hàm

def function_name[var1, var2, ...]:
        for i in 1:10
                if line1:
                       do this
                elif line2:
                       do that
                else:
                       do none
        return[result]

Áp dụng chức năng

f = lambda x: x*2
df.apply[f]
df.applymap[f] # element wise

tóm tắt

df.groupby[by='col'].function[] # function = sum[] ...
df.groupby[by='col'].agg[function] # function = sum[] ...
df.groupby[by='col'].size[]

Tham gia

pd.merge[df1, df2, how = 'left/right/inner/outer', on='col']
df[~df['col'].isin[df2['col']]] # anti-join

Chuỗi phương pháp

df = [pd.melt[df]
         .rename[columns={
                       'variable' : 'var',
                       'value' : 'val'}]
         .query['val >= 200']
        ]

Biến/cột mới

df['Volume'] = df.Length  df.Height  df.Depth
pd.qcut[df.col, n, labels=False] # binning

định hình lại dữ liệu

pd.melt[df] # cols into rows
pd.pivot[columns='var', values='val'] # rows into cols
pd.concat[[df1, df2]] # stack two dfs

con trăn

Python có tốt cho việc sắp xếp dữ liệu không?

Ngoài ra, Python có cú pháp đơn giản, dễ hiểu. Python có thể thực hiện phân tích dữ liệu, phát triển web, tự động hóa, viết kịch bản, kiểm tra phần mềm, tạo nguyên mẫu, cấu trúc dữ liệu cấp cao, sắp xếp dữ liệu và quét dữ liệu, trong số các nhiệm vụ khác

Thư viện Python nào được sử dụng để sắp xếp dữ liệu?

Gấu trúc. Pandas [Phân tích dữ liệu Python] là điều bắt buộc trong vòng đời của khoa học dữ liệu. Đây là thư viện Python phổ biến và được sử dụng rộng rãi nhất cho khoa học dữ liệu, cùng với NumPy trong matplotlib.

Pandas có được sử dụng để sắp xếp dữ liệu không?

Sắp xếp lại dữ liệu là một chủ đề quan trọng đối với Khoa học dữ liệu và Phân tích dữ liệu. Pandas Framework của Python được sử dụng để sắp xếp dữ liệu . Pandas là một thư viện mã nguồn mở được phát triển riêng cho Phân tích dữ liệu và Khoa học dữ liệu.

Bảng cheat Pandas là gì?

Bảng cheat Pandas sẽ hướng dẫn bạn thông tin cơ bản về thư viện Pandas, đi từ cấu trúc dữ liệu đến I/O, lựa chọn, bỏ chỉ mục hoặc cột, sắp xếp và xếp hạng, truy xuất thông tin cơ bản của cấu trúc dữ liệu bạn đang làm việc để áp dụng

Chủ Đề