Home > Blockchain >  Print rows of df as columns
Print rows of df as columns

Time:02-01

I have a data frame that looks like this:

category    ID  values
       A   foo  ABCDEF
       A   baz  GHIJKL
       B   bar  MNOPQR
       B  biff  STUVWX
       C   bop  YZABCD

All of the values are the same length.

I'd like to print the ID and values columns into a csv file, but printing the values as columns, so the file would like this:

foo  bar  baz  biff  bop
A    G    M    S     Y
B    H    N    T     Z
C    I    O    U     A
D    J    P    V     B
E    K    Q    W     C
F    L    R    X     D

The only method I can see is to create a numpy array with the correct lengths, fill it iteratively with the values column, then turn that into a df and use pandas print to csv method, with ID as the header. I know how to do that, but it just seems terribly inefficient.

Does anyone have a better / faster method?

CodePudding user response:

You can use this trick:

>>> df.set_index('ID')['values'].apply(lambda x: pd.Series(list(x))).T

# OR

>>> pd.DataFrame(df.set_index('ID')['values'].apply(list).to_dict())

ID foo baz bar biff bop
0    A   G   M    S   Y
1    B   H   N    T   Z
2    C   I   O    U   A
3    D   J   P    V   B
4    E   K   Q    W   C
5    F   L   R    X   D

CodePudding user response:

Lets use dict comprehension to create a mapping of ID -> values

pd.DataFrame({i: [*v] for i, v in zip(df['ID'], df['values'])})

  foo baz bar biff bop
0   A   G   M    S   Y
1   B   H   N    T   Z
2   C   I   O    U   A
3   D   J   P    V   B
4   E   K   Q    W   C
5   F   L   R    X   D

CodePudding user response:

You can do:

out = pd.DataFrame(df['values'].apply(list).tolist()).T
out.columns = df['ID']
print(out)

ID foo baz bar biff bop
0    A   G   M    S   Y
1    B   H   N    T   Z
2    C   I   O    U   A
3    D   J   P    V   B
4    E   K   Q    W   C
5    F   L   R    X   D 

CodePudding user response:

**Use pandas transpose **

df2=df.set_index('ID)
print(df2.T)

# OR

print(df2.transpose())

  • Related