Bruno Henderyckx
Bruno Henderyckx

Reputation: 13

Problems transforming a pandas dataframe

I have troubles converting a pandas dataframe into the format i need in order to analyze it further. The current data is derived from a survey where we asked people to order preferred means of communication (1=highest,4=lowest). Every row is a respondee.

The current dataframe:

    A   B   C   D
0   1   2   4   3
1   2   3   1   4
2   2   1   4   3
3   2   1   4   3
4   1   3   4   2

...

For data analysis i want to transform this into the following dataframe, where every row is a different means of communication and the columns are the counts how often a person ranked it in that spot.

   1st  2d   3th  4th
A  2    3    0    0
B  2    1    2    0
C  1    0    0    4
D  0    1    3    1

I have tried apply defined functions on the original dataframe, i have tried to apply .groupby function or .T on the dataframe with I don't seem to come closer to the result I actually want.

This is the function I wrote but I can't figure out how to apply it correctly to give me the desired result.

def count_values_rank(column,rank):
    total_count_n1 = 0
    for i in column:
        if i == rank:
            total_count_n1 += 1
    return total_count_n1

Running this piece of code on a single column of my dataframe get's the desired results but having troubles to actually write it so i can apply it to the dataframe and get the result I am looking for. The below line of code would return 2.

count_values_rank(df.iloc[:,0],'1')

It is probably a really obvious solution but having troubles seeing the easiest way to solve this.

Thanks alot!

Upvotes: 1

Views: 36

Answers (1)

BENY
BENY

Reputation: 323356

melt with crosstab

pd.crosstab(df.melt().variable,df.melt().value).add_suffix('st')
Out[107]: 
value        1st   2st   3st   4st
variable                        
A            2     3     0     0
B            2     1     2     0
C            1     0     0     4
D            0     1     3     1

Upvotes: 2

Related Questions