pandas 两列相乘 dataframe

本文介绍了如何使用Pandas创建DataFrame,并进行列操作,如新增、删除列。探讨了基于位置和标签的索引提取数据,以及数据对齐和运算。此外,还展示了如何使用布尔索引和isin方法进行复杂数据筛选。
摘要由CSDN通过智能技术生成
import numpy as np
import pandas as pd

#全部行都能输出
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

创建一个dataframe,要求索引是a,b,c,d,列名是one,two,其中one列数据为1,2,3,two列数据为1,2,3,4



data={
    'one':[1,2,3,None],'two':[1,2,3,4]}

index=['a','b','c','d']
data
{'one': [1, 2, 3, None], 'two': [1, 2, 3, 4]}
a=pd.DataFrame(data,index=index)
a
one two
a 1.0 1
b 2.0 2
c 3.0 3
d NaN 4
a.values
a.index
a.columns
a.shape
array([[ 1.,  1.],
       [ 2.,  2.],
       [ 3.,  3.],
       [nan,  4.]])






Index(['a', 'b', 'c', 'd'], dtype='object')






Index(['one', 'two'], dtype='object')






(4, 2)

查看df的索引名和列名

a.index
Index(['a', 'b', 'c', 'd'], dtype='object')
a.columns
Index(['one', 'two'], dtype='object')

列操作

给df增加一列,列名为three,列中的值为one列和two列值的乘积

a['three']=a['one']*a['two']
a
one two three
a 1.0 1 1.0
b 2.0 2 4.0
c 3.0 3 9.0
d NaN 4 NaN

分别用两种不同的方法删除df中的two和three列

a.pop('two')
a    1
b    2
c    3
d    4
Name: two, dtype: int64
a
  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值