Python: pandas.read_excel(r”路徑\檔名.副檔名”, header = None), 自動加上0,1…的欄標籤, DataFrame如何取某一直欄或橫列? 如何用 .iloc[bool_list] 取出判斷式為真的那一列? bool_list = list( df[0] == 0 ) ; bool_list = list(df[0].isin([0])) ; DataFrame如何顯示完整的資料? pandas.set_option ( “display.max_rows”, None)

加入好友
加入社群
Python: pandas.read_excel(r"路徑檔名.副檔名", header = None), 自動加上0,1...的欄標籤, DataFrame如何取某一直欄或橫列? 如何用 .iloc[bool_list] 取出判斷式為真的那一列? bool_list = list( df[0] == 0 ) ; bool_list = list(df[0].isin([0])) ; DataFrame如何顯示完整的資料? pandas.set_option ( "display.max_rows", None) - 儲蓄保險王

可參考: Python: pandas.DataFrame()處理雙維度資料

excel檔部分內容:

Python: pandas.read_excel(r"路徑檔名.副檔名", header = None), 自動加上0,1...的欄標籤, DataFrame如何取某一直欄或橫列? 如何用 .iloc[bool_list] 取出判斷式為真的那一列? bool_list = list( df[0] == 0 ) ; bool_list = list(df[0].isin([0])) ; DataFrame如何顯示完整的資料? pandas.set_option ( "display.max_rows", None) - 儲蓄保險王

如何抓出theta = 0的那一列?

 

import pandas as pd
#import openpyxl

f_hPol_ZX_Gain = r”C:\antenna_AMS\Hpol_ZX_Gain.xlsx”
# =============================================================================
# f_hPol_ZX_Phase =
#
# f_vPol_YZ_Gain =
# f_vPol_YZ_Phase =
# =============================================================================
df_hPol_ZX_Gain = pd.read_excel(f_hPol_ZX_Gain)
bool_list = list( df_hPol_ZX_Gain.iloc[:,0] == 0 )
print(bool_list)
print(df_hPol_ZX_Gain.iloc[bool_list,:])

Python: pandas.read_excel(r"路徑檔名.副檔名", header = None), 自動加上0,1...的欄標籤, DataFrame如何取某一直欄或橫列? 如何用 .iloc[bool_list] 取出判斷式為真的那一列? bool_list = list( df[0] == 0 ) ; bool_list = list(df[0].isin([0])) ; DataFrame如何顯示完整的資料? pandas.set_option ( "display.max_rows", None) - 儲蓄保險王

 

df1 = pd.read_excel(r”C:\antenna_AMS\Hpol_ZX_Gain.xlsx”, header=None)

預設值header=0, 取第0列當欄標籤

欄標籤將長如: Theta (deg) – 10700 MHz

改用 header=None

pandas自動加上0,1… 當欄標籤

Python: pandas.read_excel(r"路徑檔名.副檔名", header = None), 自動加上0,1...的欄標籤, DataFrame如何取某一直欄或橫列? 如何用 .iloc[bool_list] 取出判斷式為真的那一列? bool_list = list( df[0] == 0 ) ; bool_list = list(df[0].isin([0])) ; DataFrame如何顯示完整的資料? pandas.set_option ( "display.max_rows", None) - 儲蓄保險王

欄/列index都有0

如何取出第0欄,第0列?

第0直欄: df1[0]

第0橫列: df1.iloc[0]

Python: pandas.read_excel(r"路徑檔名.副檔名", header = None), 自動加上0,1...的欄標籤, DataFrame如何取某一直欄或橫列? 如何用 .iloc[bool_list] 取出判斷式為真的那一列? bool_list = list( df[0] == 0 ) ; bool_list = list(df[0].isin([0])) ; DataFrame如何顯示完整的資料? pandas.set_option ( "display.max_rows", None) - 儲蓄保險王

bool_list = list( df1.iloc[:,0] == 0 )

可以簡化為

bool_list = list( df1[0] == 0 )

#省略iloc

或使用.str.contains()

bool_list = list(df1[0].str.contains(“0”))

df1.astype(str).str.contains(“0”)

會誤抓到90, 80 ,70 …都含有0在裡面

#模糊搜尋

或使用isin() 函式:

bool_list = list(df1[0].isin([0]))

#isin() 裡面放的是[0]非0

#很容易誤用為數字0 #精準比對

#s=df1[0] ; s2=s.isin(0)

s2部分資料:

Python: pandas.read_excel(r"路徑檔名.副檔名", header = None), 自動加上0,1...的欄標籤, DataFrame如何取某一直欄或橫列? 如何用 .iloc[bool_list] 取出判斷式為真的那一列? bool_list = list( df[0] == 0 ) ; bool_list = list(df[0].isin([0])) ; DataFrame如何顯示完整的資料? pandas.set_option ( "display.max_rows", None) - 儲蓄保險王

Python: pandas.read_excel(r"路徑檔名.副檔名", header = None), 自動加上0,1...的欄標籤, DataFrame如何取某一直欄或橫列? 如何用 .iloc[bool_list] 取出判斷式為真的那一列? bool_list = list( df[0] == 0 ) ; bool_list = list(df[0].isin([0])) ; DataFrame如何顯示完整的資料? pandas.set_option ( "display.max_rows", None) - 儲蓄保險王

Numpy uses a custom boolean type which differs from the base bool type.

you might have to cast your numpy comparison to bool

afterwards to ensure expected behaviour.

Python bool 和 numpy bool_ 的行为究竟有何不同?

 

pandas.DataFrame.isin (官網)

或參考pandas isin() 是否包含指定值

isin() 适用的对象有:

  • pandas.DataFrame.isin
  • pandas.Series.isin
  • pandas.Index.isin
  • pandas.api.extensions.ExtensionArray.isin

只有一個参数 values 的类型及匹配规则:

  • iterable: 可迭代对象,如列表、元组、集合list-like 数据,字符串不支持。DataFrame 中的值只要与 values 其中一个值相等则匹配成功
  • Series:DataFrame 中的值只要在 Series 值中,则匹配成功。逻辑同 iterable
  • DataFrame:索引和列标签进行匹配,对应值是否相等,即同行列索引对应位置是否相等,不对应的行表索引不进行匹配
  • dict:则键必须是列名,列名必须匹配,值与对应的列上的对应位置值一一进行匹配,原 DataFrame 中不存在键名的列时不执行匹配

欄數或列數過多的話

pandas會以…省略

如何完整顯示?

pd.set_option(“display.max_columns”, None)

pd.set_option(“display.max_rows”, None)

第二個參數None改為整數n

即為設定最多顯示n欄(列)

 

恢復為預設值:

pd.reset_option(“all”)

 

顯示所有可供使用的 options:

pd.describe_option()

推薦hahow線上學習python: https://igrape.net/30afN

加入好友
加入社群
Python: pandas.read_excel(r"路徑檔名.副檔名", header = None), 自動加上0,1...的欄標籤, DataFrame如何取某一直欄或橫列? 如何用 .iloc[bool_list] 取出判斷式為真的那一列? bool_list = list( df[0] == 0 ) ; bool_list = list(df[0].isin([0])) ; DataFrame如何顯示完整的資料? pandas.set_option ( "display.max_rows", None) - 儲蓄保險王

儲蓄保險王

儲蓄險是板主最喜愛的儲蓄工具,最喜愛的投資理財工具則是ETF,最喜愛的省錢工具則是信用卡

You may also like...

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *