简体   繁体   中英

Calculate quantile for each observation in a dataframe

I am new to Python and I have the following dataframe structure:

data = {'name': ["a","b","c","d","e","f","g","h"], 'value1': [1,2,3,4,5,6,7,8],'value2': [1,2,3,4,5,6,7,8]}
data = pd.DataFrame.from_dict(data)
data = data.transpose()

What I want to calculate is a new dataframe, where for each row, each column has a value corresponding to the quantile in the data.

In other words, I am trying to understand how to apply the function pd.quantile to return a dataframe with each entry being equal to the quantile value of the column in the row.

I tried the following, but I don't think it works:

x.quantile(q = 0.9,axis =0)

or:

x.apply(quantile,axis=0)

Many thanks in advance.

This is because you transpose your data and as per pandas documentation here https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.transpose.html

When the DataFrame has mixed dtypes, we get a transposed DataFrame with the object dtype

Your dataframe after loading looks like below, which means it has 'mixed dtypes' (one column is object / category and the other two are integers).

  name  value1  value2
0    a       1       1
1    b       2       2
2    c       3       3
3    d       4       4
4    e       5       5
5    f       6       6
6    g       7       7
7    h       8       8

In this case you transpose your data and it is being converted to object dtype, which means that quantile function does not understand it as numbers.

Try removing transposing step and use axis argument to decide for which direction you want to calculate quantiles.

By the way, you can do transposition with:

df = df.T

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM