pandas中常用操作

pandas中计算相关系数

DataFrame.corr(method=’pearson’, min_periods=1)

Series中频数统计value_counts

Series.value_counts()

pandas中计算min和max

Series

DataFrame

pandas中循环迭代

Series

DataFrame

pandas选择除某几列之外的列

pandas中index的顺序

方法一

方法二

# 排序行

df = df.reindex(index=df.index.sort_values())

# 排序列

df = df.reindex(columns=df.columns.sort_values())

pandas中排序

Series

DataFrame

pandas中shuffle数据

DataFrame指定行或列进行操作

df[‘name’]=df[‘name’].apply(func)

pandas中DataFrame去重

pandas.drop_duplicates()

Series转DataFrame

(有用)DataFrame转dict

import pandas as pd

if __name__ == "__main__":
    dict_list = [{"col_1": "a", "col_2": "2"}, {"col_1": "b", "col_2": "4"}, {"col_1": "c", "col_2": "6"}]

    df = pd.DataFrame(dict_list, index=["row_1", "row_2", "row_3"])
    print(df)
    print()

    print(">>>dict")
    print(df.to_dict(orient="dict"))
    print(">>>list")
    print(df.to_dict(orient="list"))
    print(">>>series")
    print(df.to_dict(orient="series"))
    print(">>>split")
    print(df.to_dict(orient="split"))
    print(">>>records")
    print(df.to_dict(orient="records"))
    print(">>>index")
    print(df.to_dict(orient="index"))

输出结果如下:

      col_1 col_2
row_1     a     2
row_2     b     4
row_3     c     6


>>>dict
{'col_1': {'row_1': 'a', 'row_2': 'b', 'row_3': 'c'}, 'col_2': {'row_1': '2', 'row_2': '4', 'row_3': '6'}}

>>>list
{'col_1': ['a', 'b', 'c'], 'col_2': ['2', '4', '6']}

>>>series
{'col_1': row_1    a
row_2    b
row_3    c
Name: col_1, dtype: object, 'col_2': row_1    2
row_2    4
row_3    6
Name: col_2, dtype: object}

>>>split
{'index': ['row_1', 'row_2', 'row_3'], 'columns': ['col_1', 'col_2'], 'data': [['a', '2'], ['b', '4'], ['c', '6']]}

>>>records
[{'col_1': 'a', 'col_2': '2'}, {'col_1': 'b', 'col_2': '4'}, {'col_1': 'c', 'col_2': '6'}]

>>>index
{'row_1': {'col_1': 'a', 'col_2': '2'}, 'row_2': {'col_1': 'b', 'col_2': '4'}, 'row_3': {'col_1': 'c', 'col_2': '6'}}

pandas中随机采样部分行

DataFrame.sample为不放回采样

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wugou2014

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值