democracii
democracii

Reputation: 89

How to paste transposed data to new rows in Pandas Excel

Pls let me know Pandas in excel.

    A   B   C
0   1   4   7
1   2   5   8
2   3   6   9

I want to select B column value 4, 5, 6

and paste to another excel sheet with row direction. Please help me,,

B   4   5   6

Upvotes: 0

Views: 604

Answers (1)

Scott Boston
Scott Boston

Reputation: 153500

Try this. Use column filtering to select 'B' then convert the pd.Series to a dataframe using to_frame and then transpose, T. Lastly, use to_excel:

df['B'].to_frame().T.to_excel('b.xlsx')

Output:

enter image description here

Per comment below

Use startrow and startcol parameters:

df['B'].to_frame().T.to_excel('b1.xlsx', startrow=2, startcol=2)

Output:

enter image description here

Different Excel sheet:

df['B'].to_frame().T.to_excel('b3.xlsx', startrow=2, startcol=2, sheet_name='SheetNew')

Upvotes: 2

Related Questions