dataframe在最后新增一行_padans给Dataframe插入新增列、行

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 15:43   143   0

import pandas as pd

df1 = pd.DataFrame([['Snow','M',22],['Tyrion','M',32],['Sansa','F',18],['Arya','F',14]], columns=['name','gender','age'])

# 新增一列相同的数据

df1['advertiser_id'] = 'adv3282638354816'

# ----------在最后新增一列---------------

# -------案例1----------

>>> # 在数据框最后加上score一列,元素值分别为:80,98,67,90

... df1['score']=[80,98,67,90] # 增加列的元素个数要跟原数据列的个数一样

>>> print(df1)

name gender age score

0 Snow M 22 80

1 Tyrion M 32 98

2 Sansa F 18 67

3 Arya F 14 90

# -------案例2----------

# ---------在指定位置新增列:用insert()--------

# 在gender后面加一列城市

# 在具体某个位置插入一列可以用insert的方法

# 语法格式:列表.insert(index, obj)

# index --->对象 obj 需要插入的索引位置。

# obj ---> 要插入列表中的对象(列名)

>>> col_name=df1.columns.tolist() # 将数据框的列名全部提取出来存放在列表里

>>> print(col_name)

['name', 'gender', 'age', 'score']

>>> col_name.insert(2,'city') # 在列索引为2的位置插入一列,列名为:city,刚插入时不会有值,整列都是NaN

>>> df1=df1.reindex(columns=col_name) # DataFrame.reindex() 对原行/列索引重新构建索引值

>>> df1

name gender city age score

0 Snow M NaN 22 80

1 Tyrion M NaN 32 98

2 Sansa F NaN 18 67

3 Arya F NaN 14 90

>>> df1['city']=['北京','山西','湖北','澳门'] # 给city列赋值

>>> df1

name gender city age score

0 Snow M 北京 22 80

1 Tyrion M 山西 32 98

2 Sansa F 湖北 18 67

3 Arya F 澳门 14 90

# Define a dictionary containing Students data

data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],'Height': [5.1, 6.2, 5.1, 5.2],'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}

# Convert the dictionary into DataFrame

df = pd.DataFrame(data)

# Using DataFrame.insert() to add a column

df.insert(2, "Age", [21, 23, 24, 21], True)

# Observe the result

df

# Define a dictionary containing Students data

data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'], 'Height': [5.1, 6.2, 5.1, 5.2], 'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}

# Convert the dictionary into DataFrame

df = pd.DataFrame(data)

# Using 'Address' as the column name and equating it to the list

# df2 = df.assign(temp_f=lambda x: x['temp_c'] * 9 / 5 + 32,temp_k=lambda x: (x['temp_f'] + 459.67) * 5 / 9)

df2 = df.assign(address = ['Delhi', 'Bangalore', 'Chennai', 'Patna'])

# Observe the result

df2

# ----------新增行---------------

# 重要!!先创建一个DataFrame,用来增加进数据框的最后一行

>>> new=pd.DataFrame({'name':'lisa','gender':'F','city':'北京','age':19,'score':100},index=[1]) # 自定义索引为:1 ,这里也可以不设置index

>>> new

age city gender name score

1 19 北京 F lisa 100

# 没测试通过

# ignore_index=True,表示不按原来的索引,从0开始自动递增

# df = pd.DataFrame({'A': 1, 'B': 2},ignore_index=True)

# Traceback (most recent call last):

# File "", line 1, in

# TypeError: __init__() got an unexpected keyword argument 'ignore_index'

参考文章

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP