pandas-15 df['one_col'].apply()方法的用法

pandas-15 df['one_col'].apply()方法的用法

apply有点像map的用法,可以传入一个函数。
如:df[‘A’].apply(str.upper)

import numpy as np
import pandas as pd
from pandas import Series, DataFrame

df = pd.read_csv('apply_demo.csv').head()
print(df.size)  # .size 如果是series返回行数,如果是dataframe返回行数乘以列数
print(df)
''' 原始数据
         time                                data
0  1473411962   Symbol: APPL Seqno: 0 Price: 1623
1  1473411962   Symbol: APPL Seqno: 0 Price: 1623
2  1473411963   Symbol: APPL Seqno: 0 Price: 1623
3  1473411963   Symbol: APPL Seqno: 0 Price: 1623
4  1473411963   Symbol: APPL Seqno: 1 Price: 1649
'''
s1 = Series(['a'] * 5)
df['A'] = s1
print(df)
'''
         time                                data  A
0  1473411962   Symbol: APPL Seqno: 0 Price: 1623  a
1  1473411962   Symbol: APPL Seqno: 0 Price: 1623  a
2  1473411963   Symbol: APPL Seqno: 0 Price: 1623  a
3  1473411963   Symbol: APPL Seqno: 0 Price: 1623  a
4  1473411963   Symbol: APPL Seqno: 1 Price: 1649  a
'''

df['A'] = df['A'].apply(str.upper)
print(df)
'''
         time                                data  A
0  1473411962   Symbol: APPL Seqno: 0 Price: 1623  A
1  1473411962   Symbol: APPL Seqno: 0 Price: 1623  A
2  1473411963   Symbol: APPL Seqno: 0 Price: 1623  A
3  1473411963   Symbol: APPL Seqno: 0 Price: 1623  A
4  1473411963   Symbol: APPL Seqno: 1 Price: 1649  A
'''

l = df['data'][0].strip().split(' ')
print(l) # ['Symbol:', 'APPL', 'Seqno:', '0', 'Price:', '1623']

def foo(line):
    items = line.strip().split(' ')
    return Series([items[1], items[3], items[5]])

df_tmp = df['data'].apply(foo)
print(df_tmp)
'''
      0  1     2
0  APPL  0  1623
1  APPL  0  1623
2  APPL  0  1623
3  APPL  0  1623
4  APPL  1  1649
'''
df_tmp = df_tmp.rename(columns = {0:'Symbol', 1:'Seqno', 2:'Price'})
print(df_tmp)
'''
  Symbol Seqno Price
0   APPL     0  1623
1   APPL     0  1623
2   APPL     0  1623
3   APPL     0  1623
4   APPL     1  1649
'''

print(df.combine_first(df_tmp).drop(['data', 'A'], axis=1))
'''
   Price  Seqno Symbol        time
0  1623.0    0.0   APPL  1473411962
1  1623.0    0.0   APPL  1473411962
2  1623.0    0.0   APPL  1473411963
3  1623.0    0.0   APPL  1473411963
4  1649.0    1.0   APPL  1473411963
'''
df.combine_first(df_tmp).drop(['data', 'A'], axis=1).to_csv('./demo_duplicate.csv', index=False)

转载于:https://www.cnblogs.com/wenqiangit/p/11252800.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值