繁体   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