Skip to content

Instantly share code, notes, and snippets.

@onewesong
Last active September 15, 2020 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onewesong/e097961ae3a951c3ba3814a1c5396cac to your computer and use it in GitHub Desktop.
Save onewesong/e097961ae3a951c3ba3814a1c5396cac to your computer and use it in GitHub Desktop.
jupyter_cheat_sheet

pandas 常用

  • 直接拷贝表格传入notebook
pd.read_clipboard()
  • 读取xls文件转成csv文件
pd.ExcelFile(xls_fpath).parse().to_csv(csv_fpath)
  • 从字符串解析xls、csv、dict
pd.read_excel()
pd.read_csv()
pd.DataFrame.from_dict()
  • 设置列名
df_src.columns = ['a', 'b']
  • df操作列
df['xxx'].apply(lambda x:x)
  • df删除列
df.drop(columns='xxx')
  • df去重
df.drop_duplicates()
  • df合并
df.merge(df2, how='inner')  # how 选择合并方式,默认inner, 即只保留交集
  • df迭代
for index, rows in df.iterrows():
    print(index, rows.xxx)  # rows类型类似nametuple
    break
  • dict转df
pd.DataFrame.from_dict(xxx)
  • df输出不折叠、隐藏
pd.set_option('display.max_columns', None)  
pd.set_option('display.max_rows', None)
pd.set_option('display.expand_frame_repr', False)
pd.set_option('max_colwidth', None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment