python列表 行列选择_Python第七课——如何选取excel表格的行数据和列数据
# Section0
print("-"*30 + "Begin Section 0 開場(chǎng)" + "-"*30)
print("lesson7 數(shù)據(jù)選擇")
print("1.列選擇\n2.行選擇\n3.行列選擇")
print("-"*30 + "End Section 0 開場(chǎng)" + "-"*30)
import pandas as pd
# Section1 列
df = pd.read_excel(r"D:/Users/sf_xiaowei_lin/PycharmProjects/pythonProject/venv/Lesson7.xlsx")
#定義行標(biāo)
df.index = ["one","two","three","four","five"]
print("readload content of table :")
print(df,'\n')
# 只選擇名稱列
print("只選擇名稱列:")
print(df["名稱"],'\n')
# 選擇名稱列和觀看次數(shù)
print("選擇名稱列和觀看次數(shù):")
print(df[["名稱","觀看次數(shù)"]],'\n')
#選擇指定列,:冒號(hào)表示所有行,后面表示第幾列
print("選擇指定列:")
print(df.iloc[:,[0,2]],'\n')
#選擇連續(xù)的列
print("選擇第一列到第四列(不包括第四列):")
print(df.iloc[:,0:3],'\n')
# Section2 select row
# select only first row
print("select only first row :")
print(df.loc["one"],'\n')
# select first row and third row
print("select first row and third row :")
print(df.loc[["one","three"]],'\n')
# 通過第幾行來(lái)選擇
print("通過第幾行來(lái)選擇 :")
print(df.iloc[0],'\n')
# 通過行數(shù)選擇第一行和第三行
print("通過行數(shù)選擇第一行和第三行 :")
print(df.iloc[[0,2]],'\n')
# 選擇連續(xù)的行
print("選擇連續(xù)的行 :")
print(df.iloc[0:3],'\n')
# 選擇滿足條件的數(shù)據(jù)
# 記得多條件要加括號(hào)
print("選擇觀看數(shù)超過200&評(píng)論數(shù)超過20的數(shù)據(jù) :")
print(df[(df["觀看次數(shù)"] > 200) & (df["評(píng)論數(shù)"] > 20)],'\n')
# Section 3 : select row and colum at same time
# loc傳入行/列索引名稱,iloc行/列數(shù)
print("行列都用索引名稱 :")
print(df.loc[["one","three"],["名稱","觀看次數(shù)"]],'\n')
print("行和列都是索引數(shù) :")
print(df.iloc[[0,2],[0,1]],'\n')
print("行列中有切片索引 :")
print(df.iloc[0:3,[0,1]],'\n')
# 根據(jù)條件來(lái)選擇行列
print("根據(jù)條件來(lái)選擇行列 :")
print(df[df['觀看次數(shù)'] > 240][['名稱','觀看次數(shù)']],'\n')
Lesson7.xlsx內(nèi)容如下
總結(jié)
以上是生活随笔為你收集整理的python列表 行列选择_Python第七课——如何选取excel表格的行数据和列数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: session共享mysql_多服务器s
- 下一篇: 云数据仓库 Snowflake 第四季度