gpt4 book ai didi

python - 向 Pandas describe() 方法添加范围

转载 作者:行者123 更新时间:2023-12-03 08:24:30 26 4
gpt4 key购买 nike

我正在尝试这样做

这个原始的 Pandas 描述方法

       sepal_len    sepal_wid   petal_len   petal_wid
count 5.000000 5.000000 5.000000 5.0
mean 4.860000 3.280000 1.400000 0.2
std 0.207364 0.258844 0.070711 0.0
min 4.600000 3.000000 1.300000 0.2
25% 4.700000 3.100000 1.400000 0.2
50% 4.900000 3.200000 1.400000 0.2
75% 5.000000 3.500000 1.400000 0.2
max 5.100000 3.600000 1.500000 0.2

我需要在描述方法中添加Range Row

range   0.5          0.60        0.19       0.0
       sepal_len    sepal_wid   petal_len   petal_wid
count 5.000000 5.000000 5.000000 5.0
mean 4.860000 3.280000 1.400000 0.2
std 0.207364 0.258844 0.070711 0.0
min 4.600000 3.000000 1.300000 0.2
25% 4.700000 3.100000 1.400000 0.2
50% 4.900000 3.200000 1.400000 0.2
75% 5.000000 3.500000 1.400000 0.2
max 5.100000 3.600000 1.500000 0.2
range 0.5 0.60 0.19 0.0

我执行的以下代码

ds = data.head().describe()
rangelist = [];
for d in range(4):
Max = ( ds.iloc[:,d].loc['max'])
Min = ( ds.iloc[:,d].loc['min'])
rangelist.append(Max-Min)

ds.append(rangelist)

输出给我的是列值,而不是行值

0       petal_len   petal_wid   sepal_len   sepal_wid
count NaN 5.000000 5.0 5.000000 5.000000
mean NaN 1.400000 0.2 4.860000 3.280000
std NaN 0.070711 0.0 0.207364 0.258844
min NaN 1.300000 0.2 4.600000 3.000000
25% NaN 1.400000 0.2 4.700000 3.100000
50% NaN 1.400000 0.2 4.900000 3.200000
75% NaN 1.400000 0.2 5.000000 3.500000
max NaN 1.500000 0.2 5.100000 3.600000
0 0.5 NaN NaN NaN NaN
1 0.6 NaN NaN NaN NaN
2 0.2 NaN NaN NaN NaN
3 0.0 NaN NaN NaN NaN

最佳答案

你可以在一行中使用loc

ds.loc['range'] = ds.loc['max'] - ds.loc['min']
print(ds)
sepal_len sepal_wid petal_len petal_wid
count 5.000000 5.000000 5.000000 5.0
mean 4.860000 3.280000 1.400000 0.2
std 0.207364 0.258844 0.070711 0.0
min 4.600000 3.000000 1.300000 0.2
25% 4.700000 3.100000 1.400000 0.2
50% 4.900000 3.200000 1.400000 0.2
75% 5.000000 3.500000 1.400000 0.2
max 5.100000 3.600000 1.500000 0.2
range 0.500000 0.600000 0.200000 0.0

关于python - 向 Pandas describe() 方法添加范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66960952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com