[英]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.