Pandas杂杂的笔记

2020-04-17  本文已影响0人  辰雨蒋_python

0. Background

在Pandas学习过程中的一些使用的案例和想法
等杂杂笔记堆放太满了,再重新整理吧。现在就简单堆叠一下,上面放感觉新鲜的,末尾放基础的。

下面链接是比较值得过一下的资料:
pandas官方文档
实验楼的Pandas百题大冲关 非常适合pandas学差不多了,过一遍巩固。

1. 这部分放依然有新鲜感的

pd.merge(left = , right =, left_index=Ture,right_index=True, suffixes("","_right"))
  dic = {"旧值1":"新值1", "旧值2":"新值1"}
  df["col_new"] = df["col_old"].map(dic)
df = pd.DataFrame({'name': ['Alice', 'Bob', 'Candy', 'Dany', 'Ella',
                            'Frank', 'Grace', 'Jenny'],
                   'grades': [58, 83, 79, 65, 93, 45, 61, 88]})
def choice(x):
    if x > 60:
        return 1
    else:
        return 0
df.grades = pd.Series(map(lambda x: choice(x), df.grades))
def rule(x,y):
  if x == "high" and y > 10:
    return 1
  else:
    return 0
df["new"] = df.apply(lambda x: rule(x["C1"],x["C2"]), axis=1)
  df_filter= df["ID"].isin([.......])
  df[df_filter]
A.intersection(B)
A.difference(B)
# 建议可以用set(A).intersection(set(B))先去重
df = pd.DataFrame(np.random.random(size=(5, 3)))
df.sub(df.mean(axis=1), axis=0)    (算点收益率什么的应该会比较方便把)
1. 建立时间序列索引
dti = pd.date_range(start='2018-01-01', end='2018-12-31', freq='D')
data = np.random.rand(len(dti))
s = pd.Series(data, index=dti)
s
2018-01-01    0.991481
2018-01-02    0.988274
                ...
2018-12-30    0.307693
2018-12-31    0.768305
2. 统计s中的每个月的mean
s.resample('M').mean()
2. 时间转换
s = pd.date_range('today', periods=100, freq='S')
ts = pd.Series(data, index=s)
ts.resample('Min').sum() # 秒转分钟
def normalization(df):
    numerator = df.sub(df.min())
    denominator = (df.max()).sub(df.min())
    Y = numerator.div(denominator)
    return Y
df = pd.DataFrame(np.random.random(size=(5, 3)))
-----------------------------------------------------------------------
          0         1         2   #原数据
0  0.068466  0.942762  0.795099
1  0.831463  0.028050  0.603929
2  0.136350  0.826835  0.865010
3  0.595750  0.247326  0.328010
4  0.425220  0.209596  0.154659
------------------------------------------------------------------
normalization(df)
          0         1         2   #normalization后
0   0.000000    1.000000    0.901582
1   1.000000    0.000000    0.632463
2   0.088971    0.873265    1.000000
3   0.691070    0.239722    0.244036
4   0.467569    0.198474    0.000000

2. 这部分放已经看厌倦了但是先留着偶尔瞄一下防止失忆的

df = df.drop("old_col", axis=1)
df = df.join( df["new_col"] )

3. 这部分随便堆点东西

上一篇 下一篇

猜你喜欢

热点阅读