(Pandas)DataFrame常用操作

1.加载库

import numpy as np
import pandas as pd
from pandas import Series,DataFrame

2.通过browser库建立DataFrame

import webbrowser
link = 'http://pandas.pydata.org/pandas-docs/version/0.20/io.html'
webbrowser.open(link)
#进入网页,右键复制表格
df = pd.read_clipboard()
df

输出如下的DataFrame
df

3.查看DataFrame的相关量

type(df)  #查看
>>>pandas.core.frame.DataFrame
df.columns
>>>Index(['Format Type', 'Data Description', 'Reader', 'Writer'], dtype='object')
df.Reader  #选择某一列
>>>
0           read_csv
1          read_json
2          read_html
3     read_clipboard
4         read_excel
5           read_hdf
6       read_feather
7       read_msgpack
8         read_stata
9           read_sas
10       read_pickle
11          read_sql
12          read_gbq
Name: Reader, dtype: object

df['Data Description']   #列名有空格的访问办法
>>>
0                      CSV
1                     JSON
2                     HTML
3          Local clipboard
4                 MS Excel
5              HDF5 Format
6           Feather Format
7                  Msgpack
8                    Stata
9                      SAS
10    Python Pickle Format
11                     SQL
12        Google Big Query
Name: Data Description, dtype: object

type(df['Data Description'])  #只取一列的数据类型
>>>pandas.core.series.Series

4.只取原DataFrame的一部分创建新的DataFrame

df_new = DataFrame(df, columns=['Data Description', 'Reader'])
df_new

在这里插入图片描述

df_new = DataFrame(df, columns=['Data Description', 'Reader',"2018"])
df_new   #增加一列原先DF不存在的列

在这里插入图片描述

#给新增加的列赋值
df_new['2018'] = range(0,13)  ##等同于df_new['2018'] = np.arange(0,13)
df_new

在这里插入图片描述

df_new['2018'] = pd.Series([100, 200], index=[1,2])   #某几个值进行赋值
df_new

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值