英文:
how to remove unwanted zero in the output using pandas
问题
我得到的输出是:
```none
第一四分位数是 21.0
第二四分位数是 22.0
第三四分位数是 23.25
IQR 年龄是 1.125
字符串和变量之间有零的打印
为什么会出现这些0,并且如何从输出中删除它们。
我尝试了好几次,但是这个问题没有得到解决。```
英文:
for the below code:
age_batch=[15,16,20,21,21,
21,21,21,21,21,
21,21,21,21,21,
21,21,21,22,22,
22,22,22,22,22,
23,23,23,23,23,
24,24,24,26,26,
27,27,28,35,56]
import pandas as pd
data= pd.DataFrame(age_batch)
q1=data.quantile(0.25)
q2=data.quantile(0.5)
q3=data.quantile(0.75)
IQR=(q3-q1)/2
print("first quartile is",q1.to_string())
print("second quartile is ", q2.to_string())
print("third quartile is ", q3.to_string())
print("IQR Age is ", IQR.to_string())
I am getting output as:
first quartile is 0 21.0
second quartile is 0 22.0
third quartile is 0 23.25
IQR Age is 0 1.125
there are zero printing in b/w the the string and variable<br>
why these 0's are coming and how to remove this from the output.
I tried several time but this issue is not getting resolved
答案1
得分: 0
你面临的问题是因为你使用了DataFrame来存储你的数值。当你从DataFrame中提取单个值(如q1、q2、q3或IQR)时,它会保留索引。你看到的不想要的0是提取的单个值的索引。
要解决这个问题并删除输出中的不想要的零,你可以使用.iloc
属性将提取的值转换为标量值,以访问第一个(也是唯一的)元素。以下是如何修改你的代码来实现这一点:
import pandas as pd
age_batch = [15, 16, 20, 21, 21,
21, 21, 21, 21, 21,
21, 21, 21, 21, 21,
21, 21, 21, 22, 22,
22, 22, 22, 22, 22,
23, 23, 23, 23, 23,
24, 24, 24, 26, 26,
27, 27, 28, 35, 56]
data = pd.DataFrame(age_batch)
q1 = data.quantile(0.25).iloc[0]
q2 = data.quantile(0.5).iloc[0]
q3 = data.quantile(0.75).iloc[0]
IQR = (q3 - q1) / 2
print("第一四分位数是", q1)
print("第二四分位数是", q2)
print("第三四分位数是", q3)
print("IQR 年龄是", IQR)
输出
第一四分位数是 21.0
第二四分位数是 22.0
第三四分位数是 23.25
IQR 年龄是 1.125
英文:
The issue you're facing is due to the fact that you're using a DataFrame to store your values. When you extract a single value (like q1, q2, q3, or IQR) from the DataFrame, it retains the index. The unwanted 0 that you're seeing is the index of the single value extracted.
To resolve this and remove the unwanted zeros in the output, you can convert the extracted values to scalar values using the .iloc property to access the first (and only) element. Here's how you can modify your code to achieve this:
import pandas as pd
age_batch = [15, 16, 20, 21, 21,
21, 21, 21, 21, 21,
21, 21, 21, 21, 21,
21, 21, 21, 22, 22,
22, 22, 22, 22, 22,
23, 23, 23, 23, 23,
24, 24, 24, 26, 26,
27, 27, 28, 35, 56]
data = pd.DataFrame(age_batch)
q1 = data.quantile(0.25).iloc[0]
q2 = data.quantile(0.5).iloc[0]
q3 = data.quantile(0.75).iloc[0]
IQR = (q3 - q1) / 2
print("first quartile is", q1)
print("second quartile is", q2)
print("third quartile is", q3)
print("IQR Age is", IQR)
output
first quartile is 21.0
second quartile is 22.0
third quartile is 23.25
IQR Age is 1.125
答案2
得分: 0
你的数据框中的索引列的四分位数结果在开始时都是零。
一个快速的解决方法是通过使用索引来从数据框中仅获取所需的列,例如 -- q1[0]。
import pandas as pd
data = pd.DataFrame(age_batch)
q1 = data.quantile(0.25)
q2 = data.quantile(0.5)
q3 = data.quantile(0.75)
IQR = (q3 - q1) / 2
print("第一四分位数是", q1[0])
print("第二四分位数是", q2[0])
print("第三四分位数是", q3[0])
print("IQR 年龄是", IQR[0])
输出:
第一四分位数是 21.0
第二四分位数是 22.0
第三四分位数是 23.25
IQR 年龄是 1.125
英文:
You are getting zero in starting as the quartile result for the index column from your data frame.
A quick fix will be to take out only desired column from the data frame by using indexing like -- q1[0].
import pandas as pd
data= pd.DataFrame(age_batch)
q1=data.quantile(0.25)
q2=data.quantile(0.5)
q3=data.quantile(0.75)
IQR=(q3-q1)/2
print("first quartile is", q1[0])
print("second quartile is ", q2[0])
print("third quartile is ", q3[0])
print("IQR Age is ", IQR[0])
Output
first quartile is 21.0
second quartile is 22.0
third quartile is 23.25
IQR Age is 1.125
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论