簡體   English   中英

從 python 中的打印 output 中刪除索引、列名和“dtype”(熊貓數據框)

[英]Remove Index, column name and "dtype" from print output in python (pandas dataframe)

我正在使用 pandas 處理 python 中的數據集當我執行任務並打印時,output 帶有一些不必要的行,例如

  1. 行索引
  2. 列名
  3. “數據類型”:object

我不想要這些。 例如;

我有這個代碼來提取案例最多的國家

tmax= df_covid["totcases"].max()
tmin = df_covid["totcases"].min()
dfMax=df_covid.loc[df_covid['totcases'] == tmax, 'Country/Region']
dfMin=df_covid.loc[df_covid['totcases'] == tmin, 'Country/Region']

print(f"The country with the highest number of total cases is: {dfMax} with {tmax} total cases")

output:

The country with the highest number of total cases is: 0    USA
Name: Country/Region, dtype: object with 5032179 total cases

我想要的 Output

The country with the highest number of total cases is: USA with 5032179 total cases

謝謝

df_covid.loc在您的情況下(當為行 label 指定 boolean 數組並為列指定單個 label 時)將返回pd.Series object,因此您需要直接通過values屬性訪問結果值:

tmax = df_covid["totcases"].max()
tmin = df_covid["totcases"].min()
dfMax = df_covid.loc[df_covid['totcases'] == tmax, 'Country/Region'].values[0]
dfMin = df_covid.loc[df_covid['totcases'] == tmin, 'Country/Region'].values[0]

print(f"The country with the highest number of total cases is: {dfMax} with {tmax} total cases")
tmax= df_covid["totcases"].max()
tmin = df_covid["totcases"].min()
dfMax=df_covid.loc[df_covid['totcases'] == tmax, 'Country/Region']
dfMin=df_covid.loc[df_covid['totcases'] == tmin, 'Country/Region']

print(f"The country with the highest number of total cases is: {dfMax.iloc[0].values} with {tmax} total cases")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM