Copy range of values from one Dataframe to another transposed

Asked

Viewed 1,052 times

0

I would like to copy multi-range values in a df1 with 721 rows and multiple columns, and paste in a df2 transposed form.

Something like that:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

I tried something like:

df1.T

 df2.iloc[0,1:18]=df1.iloc[0,0:17]

 df2

But it didn’t work...

  • Dataframe and this method iloc are from Pandas, right?! It would be better to add the tag [tag:pandas] in the question to facilitate the search.

  • 1

    But what you want to do is not the transposta of df original? if your representation is wrong, the transposition of the df orginal Teri that have only 2 lines and not 3.

1 answer

0

Hello, all right?

You can do as follows, see this example below:

import pandas as pd
data = [[1,2,3],[3,4,5], [5,6,7], [7,8,9]]

col = ['col_'+str(i) for i in range(len(data[0]))]
row = ['row_'+str(i) for i in range(len(data))]

df = pd.DataFrame(data, columns = col)
df.index = row
df

Result of dataframe df

inserir a descrição da imagem aqui

Hence, you can select part of this dataframe using the iloc, for example:

# df.iloc[seleciona linhas, seleciona colunas]
df2 = df.iloc[1:4,1:]
df2

Result of dataframe df2

inserir a descrição da imagem aqui

And then transposed the dataframe df2 is just to do,

df2_T = df2.T
df2_T

Date frame transpose result df2

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.