[英]Remove Index, column name and "dtype" from print output in python (pandas dataframe)
我正在使用 pandas 處理 python 中的數據集當我執行任務並打印時,output 帶有一些不必要的行,例如
我不想要這些。 例如;
我有這個代碼來提取案例最多的國家
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.