Pandas learn 2

Learning Summary:

1. Get a column in the DataFrame:

method 1:

DataFrame.columnname

Make the column name as an attribute of the DataFrame to get the data for that column

method 2:

DataFrame['column name']

Use the column name as the key to get the data for this column

2. Obtain the qualified data in the DataFrame:

DataFrame['column name'][index number]

Get the data in the DataFrame with column name and row number is index number

3. iloc:

Get the data in the first row:

DataFrame.iloc[0]

Get the first column data for each row (first column data) :

DataFrame.iloc[:,0]

Get the first column of the first three rows:

# method 1

DataFrame.iloc[:3,0]

# method 2
DataFrame.iloc[[1, 2, 3],0]

Get the last five rows:

DataFrame.iloc[-5:]

4. loc:

Gets the first row of the specified column:

DataFrame.loc[0,'column name']

Gets each row of the specified column:

DataFrame.loc[:,['column name 1', 'column name 2']]

5. Differences between iloc and loc:

The index ends at different positions. In iloc, 0:10 refers to 0,1,2,3..... 9 But the 0:10 in loc includes 10

6. set_index():

DataFrame.set_index('index name')

Add a row for the Dataframe. The index of the new row is index name 

7. Screening:

DataFrame.columnName == 'target'

Returns a series and determines whether each data above the specified column is' target '. If so, the corresponding position of the returned series is True; otherwise, False

DataFrame.loc[DataFrame.columnName == 'target']

Returns each row that meets the criteria

8. isin and notnull:

DataFrame.loc(DataFrame.columnName.isin(['data 1','data 2']))

Returns each row in the DataFrame whose columnName is columnName and whose data is data 1 or data 2

DataFrame.loc(DataFrame.columnName.notnull())

Returns each row in the DataFrame whose columnName is columnName and whose data is not null

The first parameter in loc is used to filter the rows that meet the requirements, and the second parameter is used to filter the columns that meet the requirements

Reference:

Indexing, Selecting & Assigning | Kaggle

学习汇总:

1. 获取DataFrame中的某一列:

方法一:

DataFrame.columnname

将列的名称作为DataFrame的属性来获得这一列的数据 

方法二:

DataFrame['column name']

将列名作为key来获取这一列的数据

2. 获取DataFrame中符合条件的数据:

DataFrame['column name'][index number]

获取DataFrame中满足列名为column name并且行数为index number的数据

3. iloc:

获取第一行的数据:

DataFrame.iloc[0]

获取每一行的第一列数据(第一列数据):

DataFrame.iloc[:,0]

获取前三行的第一列数据:

# method 1

DataFrame.iloc[:3,0]

# method 2
DataFrame.iloc[[0, 1, 2],0]

获取最后五行:

DataFrame.iloc[-5:]

4. loc:

获取指定列的第一行:

DataFrame.loc[0,'column name']

获取指定列的每一行:

DataFrame.loc[:,['column name 1', 'column name 2']]

5. iloc和loc的不同:

索引结束位置不同,iloc中的0:10是指0,1,2,3.....9但是loc中的0:10包括了10

6. set_index():

DataFrame.set_index('index name')

为Dataframe添加一行,新添加的一行的index为index name

7. 筛选数据:

DataFrame.cloumnName == 'target'

返回一个series,判断指定的列上面的每个数据是否为‘target’,如果是,则返回的series的对应位置为True反之则为False

DataFrame.loc[DataFrame.columnName == 'target']

 返回符合条件的每一行

8. isin和notnull:

Dataframe.loc[DataFrame.columnName.isin(['data 1','data 2'])]

返回DataFrame中列名为columnName并且这一列中数据为data 1或者data 2的每一行

DataFrame.loc(DataFrame.columnName.notnull())

返回DataFrame中列名为columnName并且这一列中数据不是null的每一行

loc中第一个参数是用来筛选满足要求的行,第二个参数是用来筛选满足要求的列

引用:

Indexing, Selecting & Assigning | Kaggle

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值