DataFrame.assign

_DataFrame.assign(kwargs)**
给DataFrame分配一个新列,返回一个新的_DataFrame
对象,不仅包含新添加的列,还包含所有原始的列。重新分配的现有列将被覆盖。

举例:使用count列和price列的乘积作为total列

  1. import pandas as pd
  2. df = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],
  3. 'count':[18, 39, 22, 45],
  4. 'price': [1.0, 2.0, 3.0, 4.0]})
  5. df = df.assign(total=df['count'] * df['price'])
  6. ------------------------------------
  7. site count price total
  8. 0 google 18 1.0 18.0
  9. 1 baidu 39 2.0 78.0
  10. 2 wiki 22 3.0 66.0
  11. 3 pandas 45 4.0 180.0

举例:为原本的site列的所有元素重新赋值

  1. import pandas as pd
  2. df = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],
  3. 'count':[18, 39, 22, 45],
  4. 'price': [1.0, 2.0, 3.0, 4.0]})
  5. df = df.assign(site=['google','google','google','google'])
  6. -------------------------------
  7. site count price
  8. 0 google 18 1.0
  9. 1 google 39 2.0
  10. 2 google 22 3.0
  11. 3 google 45 4.0

举例:将Lambda表达式的结果作为total列添加

  1. import pandas as pd
  2. df = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],
  3. 'count':[18, 39, 22, 45],
  4. 'price': [1.0, 2.0, 3.0, 4.0]})
  5. df = df.assign(total=lambda x: x['price'] / 2)
  6. -------------------------------------
  7. site count price total
  8. 0 google 18 1.0 0.5
  9. 1 baidu 39 2.0 1.0
  10. 2 wiki 22 3.0 1.5
  11. 3 pandas 45 4.0 2.0

举例:将Series对象作为新的列

  1. import pandas as pd
  2. df = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas']})
  3. s1 = pd.Series([18, 39, 22, 45], name='count')
  4. s2 = pd.Series([1, 2, 3, 4], name='price')
  5. df.assign(count=s1, price=s2)
  6. ----------------------------------------------------------------
  7. site count price
  8. 0 google 18 1
  9. 1 baidu 39 2
  10. 2 wiki 22 3
  11. 3 pandas 45 4