pd.DataFrame的 assign 方法

2021-03-02 xiao樂

最近身體不太舒服,停更幾天,萬分抱歉,望tie子們見諒。另外,在單位開始著手處理to C業務,需要處理size較大的數據,最近忙於學習、運用pandas和sql,近期總結、梳理一些常用的知識點。

本節知識點:pd.DataFrame中列的添加新column(s)常用兩種途徑

添加新column(s) — 直接賦值

比較加單,直接上code

import pandas as pd
import numpy as np
df = pd.DataFrame({'temp_c': [17.0, 25.0]} ,   
                  index=('Portland', 'Berkeley') )
df

  temp_c
Portland 17.0
Berkeley 25.0

df['temp_f']= df.temp_c * 9 / 5 + 32
# 等價於 df['temp_f']= df['temp_c'] * 9 / 5 + 32
df

         temp_c temp_f
Portland 17.0 62.6
Berkeley 25.0 77.0

直接賦值的方法,類似於dict  class的  key-value 賦值一般

如果沒有這個column,則產生新的column;如果column存在,更新該column中的value值添加新column(s) — assign method

assign method是 pd.DataFrame class 的一個內置方法,用於添加或者更新 pd.DataFrame 的column(s)

這個才是今天的重點知識點

assign的code定義
 class DataFrame(NDFrame):
        
    
    def assign(self, **kwargs) -> "DataFrame":
        r"""
        Assign new columns to a DataFrame.

        Returns a new object with all original columns in addition to new ones.
        Existing columns that are re-assigned will be overwritten.

        Parameters
        
        **kwargs : dict of {str: callable or Series}
            The column names are keywords. If the values are
            callable, they are computed on the DataFrame and
            assigned to the new columns. The callable must not
            change input DataFrame (though pandas doesn't check it).
            If the values are not callable, (e.g. a Series, scalar, or array),
            they are simply assigned.

        Returns
        --
        DataFrame
            A new DataFrame with the new columns in addition to
            all the existing columns.

        Notes
        
        Assigning multiple columns within the same ``assign`` is possible.
        Later items in '\*\*kwargs' may refer to newly created or modified
        columns in 'df'; items are computed and assigned into 'df' in order.

        .. versionchanged:: 0.23.0

           Keyword argument order is maintained.

        Examples
        ---
        >>> df = pd.DataFrame({'temp_c': [17.0, 25.0]},
        ...                   index=['Portland', 'Berkeley'])
        >>> df
                  temp_c
        Portland    17.0
        Berkeley    25.0

        Where the value is a callable, evaluated on `df`:

        >>> df.assign(temp_f=lambda x: x.temp_c * 9 / 5 + 32)
                  temp_c  temp_f
        Portland    17.0    62.6
        Berkeley    25.0    77.0

        Alternatively, the same behavior can be achieved by directly
        referencing an existing Series or sequence:

        >>> df.assign(temp_f=df['temp_c'] * 9 / 5 + 32)
                  temp_c  temp_f
        Portland    17.0    62.6
        Berkeley    25.0    77.0

        You can create multiple columns within the same assign where one
        of the columns depends on another one defined within the same assign:

        >>> df.assign(temp_f=lambda x: x['temp_c'] * 9 / 5 + 32,
        ...           temp_k=lambda x: (x['temp_f'] +  459.67) * 5 / 9)
                  temp_c  temp_f  temp_k
        Portland    17.0    62.6  290.15
        Berkeley    25.0    77.0  298.15
        """

Basic explanation

功能作用:(1)Returns a new object with all original columns in addition to new ones.  返回 pd.DataFrame,返回值保留原先dataFrame的所有column(s),同時添加新的column(s)   (2) Existing columns that are re-assigned will be overwritten. 如果 assign所涉及的column(s)已經存在,則更新column(s)中的值

參數說明:**kwargs : dict of {str: callable or Series}  The column names are keywords. If the values are callable, they are computed on the DataFrame and assigned to the new columns. The callable must not change input DataFrame (though pandas doesn't check it). If the values are not callable, (e.g. a Series, scalar, or array), they are simply assigned.

首先,使用**kwargs相當合理,參數名指定column名,參數值指定column的值

其次,允許參數值為 callable ,而不僅僅是現有的、已經計算好的數據結構,又涉及到first-class function的概念了,pandas自己做了一層包裝,將 callable 的對象作用於當前的pd.DataFrame對象,並將生成的數據賦值給新生成的column

代碼示例

代碼示例1、

import pandas as pd
import numpy as np
df = pd.DataFrame({'temp_c': [17.0, 25.0]} ,   
                  index=('Portland', 'Berkeley') )
df

  temp_c
Portland 17.0
Berkeley 25.0

df.assign(  temp_f=df['temp_c'] * 9 / 5 + 32 )

  temp_c temp_f
Portland 17.0 62.6
Berkeley 25.0 77.0

df

  temp_c
Portland 17.0
Berkeley 25.0

代碼很簡單,不多做解釋,參數名  temp_f 作為新column名,參數值df['temp_c'] * 9 / 5 + 32 作為新column中的值

注意一點:原先df不變

代碼示例2

import pandas as pd
import numpy as np
df = pd.DataFrame({'temp_c': [17.0, 25.0]} ,   
                  index=('Portland', 'Berkeley') )
df

  temp_c
Portland 17.0
Berkeley 25.0

temp_f=lambda x: x.temp_c * 9 / 5 + 32
print(temp_f(  df ))
print( type(temp_f(  df )))

Portland    62.6
Berkeley    77.0
Name: temp_c, dtype: float64
<class 'pandas.core.series.Series'>

 df.assign(temp_f=temp_f)

  temp_c temp_f
Portland 17.0 62.6
Berkeley 25.0 77.0

temp_f=lambda x: list(x.temp_c * 9 / 5 + 32)
print(temp_f(  df ))
print( type(temp_f(  df )))
print( df.assign(temp_f=df['temp_c'] * 9 / 5 + 32))

[62.6, 77.0]
<class 'list'>
          temp_c  temp_f
Portland    17.0    62.6
Berkeley    25.0    77.0

有點意思吧,注意以下三點

依舊是 參數名= 參數值,這種賦值形式,參數名用於定於新的(或者待更新的)column名

此刻temp_f為一個匿名function,該function的參數默認為當前的pd.DataFrame對象

至於這個callable怎麼定義,可以自由發揮,第一層我就簡單用了pd.DataFrame對象的四則運算,第二次我轉成了list對象,當然還可以添加更為複雜的路徑,但是通常都是當前pd.DataFrame對象幾組columns的統計性的數值

代碼示例3、

import pandas as pd
import numpy as np
df = pd.DataFrame({'temp_c': [17.0, 25.0]} ,   
index=('Portland', 'Berkeley') )
temp_f=lambda x: list(x.temp_c * 9 / 5 + 32)
temp_k = lambda x: (x['temp_f'] +  459.67) * 5 / 9
df.assign(temp_f= temp_f , temp_k = temp_k )

temp_c temp_f temp_k
Portland 17.0 62.6 290.15
Berkeley 25.0 77.0 298.15

有意思,很複雜,其實也很簡單,兩個知識點:

one of the columns depends on another one defined within the same assign,即在添加的columns中 某一列的數值可以依賴於另一列生成有意思的示例
df = pd.DataFrame({'col1':list('abcde'),'col2':range(5,10),'col3':[1.3,2.5,3.6,4.6,5.8]},
                 index=range(1,6))
df

col1 col2 col3
1 a 5 1.3
2 b 6 2.5
3 c 7 3.6
4 d 8 4.6
5 e 9 5.8

df.assign(C=pd.Series(list('def')))

col1 col2 col3 C
1 a 5 1.3 e
2 b 6 2.5 f
3 c 7 3.6 NaN
4 d 8 4.6 NaN
5 e 9 5.8 NaN

思考:為什麼會出現NaN?(提示:索引對齊)assign左右兩邊的索引不一樣,請問結果的索引誰說了算?

df = pd.DataFrame({'col1':list('abcde'),'col2':range(5,10),'col3':[1.3,2.5,3.6,4.6,5.8]},
                 index=range(1,6))
df

col1 col2 col3
1 a 5 1.3
2 b 6 2.5
3 c 7 3.6
4 d 8 4.6
5 e 9 5.8

pd.Series(list('def'))

0    d
1    e
2    f
dtype: object

df["C"] = pd.Series(list('def'))
df

col1 col2 col3 C
1 a 5 1.3 e
2 b 6 2.5 f
3 c 7 3.6 NaN
4 d 8 4.6 NaN
5 e 9 5.8 NaN

好了,我想你已經懂了

image-20200930002808374References

[1]   /pandas-docs 參考連結  https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.assign.html

Motto

日拱一卒,功不唐捐

長按下圖  關注xiao樂


相關焦點

  • Pandas-DataFrame基礎知識點總結
    根據字典創建data = {    'state':['Ohio','Ohio','Ohio','Nevada','Nevada'],    'year':[2000,2001,2002,2001,2002],    'pop':[1.5,1.7,3.6,2.4,2.9]}frame = pd.DataFrame(data)frame#輸出    pop state
  • Pandas數據結構:DataFrame
    , 18],"city": ["BeiJing", "TianJin", "ShenZhen"]}print(data)print("")frame = pd.DataFrame(data) # 創建DataFrame
  • Python-Pandas安裝--Series結構和DataFrame結構
    1)首先打開cmd終端,輸入pip install pandas(這樣可能會因為超時導致安裝失敗,不妨試一下pip --default-time=10000 install pandas)2)其實中間安裝失敗很多次,我也找了其他的解決方案,如果方法一對你來說不可取,那麼就看方法二吧,百度搜索(原文地址: http://www.wsmee.com
  • Pandas常用的兩種數據類型之「DataFrame」
    d=pd.DataFrame([[1,2,3],[11,22,33]],columns=["a","b","c"],index=["第一行","第二行"])# display(d)d=pd.DataFrame({"A":[10,20,30],"B":[40,50,60]},index=["一","二","三"])# d.index=# 關於dataframe
  • python數據分析專題 (12):DataFrame
    DataFrame既有行索引也有列索引,pandas中的DataFrame類似於R中的data.frame數據框,屬於二維數據。是數據分析中最為常用的數據類型。創建DataFrame可以使用pandas包中的DataFrame()函數生成DataFrame數據結構。有多種方式,可以直接從python的字典進行轉換,也可以從ndarry生成。
  • 數據集的「乾坤大挪移」:如何使pandas.Dataframe從行式到列式?
    有很多種方法可以做到這一點,但pandas的melt()方法是其中最靈活的一種,你只需要學好這種就足夠了。本文將傳授給你數據集的「乾坤大挪移」大法,使用pd.melt()或melt方法使pandas. dataframe發生形變。一起來看看吧!行式vs寬式數據集如果我們將一個行式數據集與列式數據集進行比較,最容易理解行式數據集是什麼樣?
  • DataFrame(4):DataFrame的創建方式
    ③ 列表組成的列表x = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]df = pd.DataFrame(data)display(df)結果如下:(data)display(df)結果如下:
  • Pandas系列 - DataFrame操作
    , index, columns, dtype, copy)編號參數描述1data數據採取各種形式,如:ndarray,series,map,lists,dict,constant和另一個DataFrame。
  • 將Python中的字典數據轉化為DataFrame
    data = {『key1』: values, 『key2』:values, 『key3』:values, …, 『keyN』:values}df = pd.DataFrame(data)這裡是將一個Python中的字典data轉化為了Pandas中的DataFrame對象,這樣字典就作為了數據源。
  • 深拷貝和淺拷貝之list、dataframe
    python dataframe:深拷貝,pd.DataFrame.copy(user_info, deep=True),原值改變,複製的新值不會改變。淺拷貝,pd.DataFrame.copy(user_info, deep=False),原值改變,複製的新值也改變。
  • pandas:Series , Data Frame , Panel
    然而,我們找到了解決這個問題的方法:在這裡,我們將使用兩種方法來獲取數據:DataReader和read\ csv函數。data['AAPL']=aaplDFprint(type(data))CSV的列分別是 「Date」,「Open」, 「High」, 「Low」, 「Close」, 「Volume」。不能為空,也不能有缺失值。
  • DataFrame(3):DataFrame的創建方式
    ]df = pd.DataFrame(data)display(df)結果如下:, "Python":95, "Hive":96},    "王五":{"Java":85, "Python":94}}df = pd.DataFrame(data)display(df)data = {    "Java":{"張三":90,"李四":82,"王五":85},    "Python":{"張三":89,"李四"
  • 直接保存 DataFrame 表格到本地,這個「騷操作」你還不知道?
    方法介紹完成這個需求使用的是dataframe_image庫,因此在使用他之前,需要我們先安裝這個庫。pip install dataframe_imageip install dataframe_imageimport dataframe_image as dfi接著,調用dfi中的export()方法,就可以實現這個需求。但是關於這個知識點,你在百度上面其實看不到任何解答,那麼你應該怎麼學習呢?
  • 數據分析利器 pandas 系列教程(二):強大的 DataFrame
    創建 dataframe 的常見方式同 series 一樣,dataframe 也有 index,不同的是,series 除了 index,只有一列,而 dataframe 通常有很多列,比如上面的 dataframe 就有四列,而且都有名字:name、sex、course、grade,通過這些名字,可以索引到某一列,這些名字稱為列(索引),因此,在 dataframe
  • paipai教你pandas(1) DataFrame的列維度操作
    今天paipai在使用pandas時,遇到了第一個問題,從一份Excel文件中讀取數據,構建為一個DataFrame對象,但是數據中的屬性欄位很多,需要對一些欄位進行取捨,paipai處理完數據之後呢,對Dataframe的列操作,整理了一下,在這裡分享給大家。      為了讓大家理解起來更直觀明了,paipai隱去複雜的列名和數值,用簡單的字母來代替。
  • 什麼是Pandas的DataFrame?
    創建DataFrame最常用的一種是直接傳入一個由等長列表或NumPy數組組成的字典:In [33]: data={'state':['Ohio','Ohio','Ohio','Nevada','Nevada'],'year':[2000,2001,2002,2001,2002],'pop':[1.5,1.7,3.6,2.4,2.9
  • pandas | 詳解DataFrame中的apply與applymap方法
    在上一篇文章當中,我們介紹了panads的一些計算方法,比如兩個dataframe的四則運算,以及dataframe填充Null的方法。今天這篇文章我們來聊聊dataframe中的廣播機制,以及apply函數的使用方法。
  • pandas.DataFrame.describe 方法介紹
    import pandas as pdimport numpy as npdata = {'玩具':['輪船','飛機','輪船'], '數量':[3,2,4], '價格':[100,90,80], '人員':[45,50,'張三'], '時間':[np.datetime64(
  • Python:Pandas的DataFrame如何按指定list排序
    在具體的分析過程中,先將pandas的Series轉換成為DataFrame,然後設置數據類型,再進行排序。思路用流程圖表示如下:>df['words'] = df['words'].astype('category')# inplace = True,使 recorder_categories生效df['words'].cat.reorder_categories(list_custom, inplace=True)# inplace = True,使 df生效