pandas取列号_pandas生成dataframe,获取行和列,得到series

本文介绍了如何在Python的pandas库中创建DataFrame,以及如何通过loc、iloc和ix方法来获取DataFrame的行和列。示例中展示了如何改变行和列的索引,并强调了获取数据后的结果类型是Series。
摘要由CSDN通过智能技术生成

lib不用就忘:

一、创建dataframe

C:\Windows\system32>python

Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> import pandas as pd

>>> a = [[3, 4], [5, 6],[7,8]]

>>> b = pd.DataFrame(a)

>>> b

0 1

0 3 4

1 5 6

2 7 8

二、索引

# dataframe的行索引为index,列索引为columns

# 改变索引,会改变dataframe本身,如果不想改变dataframe本身,最好先复制再修改。

new_index1 = ['d', 'e', 'f']

b.index=new_index1

0 1

d 3 4

e 5 6

f 7 8

# 改变列索引

new_index2 = ['g', 'h']

b.columns=new_index2

g h

0 3 4

1 5 6

<
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值