zoukankan      html  css  js  c++  java
  • pandas dataframe.apply() 实现对某一行/列进行处理获得一个新行/新列

    重点:
    dataframe.apply(function,axis)对一行或一列做出一些操作(axis=1则为对某一列进行操作,此时,apply函数每次将dataframe的一行传给function,然后获取返回值,将返回值放入一个series)
    python去空格:字符串.strip()

    待解决:
    dataframe.assign()应该怎么用?

    (1)读入数据后先把 城市 那一列城市名中的空格去掉

    对一列数据去空格的方法:

    def qukong(hang):
    return hang['city'].strip()
    dataframe['city']=dataframe.apply(qukong,axis=1) # axis=1表示对每一行做相同的操作
    dataframe

    ###dataframe.apply用于对一行或一列做一些相同的操作

    (2)调用百度地图API查询各城市经纬度(查询出的结果我们用dict保存)

    def p2l(name):
    # 1、设置url和3个参数(输出格式,key,要翻译的地址)
    url = 'http://api.map.baidu.com/geocoder/v2/'
    output = 'json'
    ak = 'sXZHPZahdMeK3Gy3uC7ZeRQrVbZDnP1G'
    address = quote(name)

    # 2、拼接get请求(url?参数1=值1&参数2=值2&参数3=值3)
    request = url + '?' + 'address=' + address + '&output=' + output + '&ak=' + ak

    # 3、urlopen发送请求,获得response
    response_file = urlopen(request)

    # 4、读取response字符串
    response_str = response_file.read().decode()

    # 5、str转json
    response_json = json.loads(response_str)

    # 6、读json
    lat=response_json['result']['location']['lat']
    lng=response_json['result']['location']['lng']

    return [lat,lng]

    list_place=list(set(dataframe['city']))
    dict_loc={}
    for elem in list_place:
    dict_loc[elem]=p2l(elem)
    dict_loc


    (3)将查询到的经纬度放入dataframe中
    def add_lat(hang):
    return dict_loc[hang['city']][0]

    def add_lng(hang):
    return dict_loc[hang['city']][1]

    dataframe['city_lat']=dataframe.apply(add_lat,axis=1)
    dataframe['city_lng']=dataframe.apply(add_lng,axis=1)

    (4)从dataframe的日期一列中提取出 年、月、日 三个新列
    def add_year(hang):
    date=hang['date']
    tmplist=date.split('/')
    return tmplist[0]

    def add_month(hang):
    date=hang['date']
    tmplist=date.split('/')
    return tmplist[1]

    def add_day(hang):
    date=hang['date']
    tmplist=date.split('/')
    return tmplist[2]

    dataframe['year']=dataframe.apply(add_year,axis=1)
    dataframe['month']=dataframe.apply(add_month,axis=1)
    dataframe['day']=dataframe.apply(add_day,axis=1)

    (5)获取星期几
    from datetime import datetime,date

    dayOfWeek = datetime.now().weekday()
    print dayOfWeek

    dayOfWeek = datetime.today().weekday()
    print dayOfWeek
    datetime类的weekday()方法可以获得datetime是星期几,注意weekday() 返回的是0-6是星期一到星期日

  • 相关阅读:
    ExecuteScalar requires the command to have a transaction when the connection assigned to the command is in a pending
    如何从vss中分离程序
    String or binary data would be truncated
    the pop3 service failed to retrieve authentication type and cannot continue
    The POP3 service failed to start because
    IIS Error he system cannot find the file specified _找不到页面
    pku2575Jolly Jumpers
    pku2940Wine Trading in Gergovia
    pku3219二项式系数
    pku1029false coin
  • 原文地址:https://www.cnblogs.com/zealousness/p/8343418.html
Copyright © 2011-2022 走看看