2

I have a dataframe thats rows are indexed properly 1:n but the columns do not have an integer index. the first row represents frequencies, which are not actual integers allowing for easy indexing.

heres what im looking at:

index 3.14 3.28 3.42 3.56
0 data data data data
1 data data data data
2 data data data data
3 data data data data
4 data data data data

heres what i want:

index 0 1 2 3
0 3.14 3.28 3.42 3.56
1 data data data data
2 data data data data
3 data data data data
4 data data data data
5 data data data data

I have this ready, but need to find an insert function that also will alllow me to convert the array into a df

 ### df size = [20000, 100]
 index_row = np.linspace(1,100,100)
 index_row = index_row.reshape(1,100)
0

1 Answer 1

1

You can use reset_index after tranposing your df.

Let's load a sample of your data:

from io import StringIO
data = StringIO(
"""
    3.14    3.28    3.42    3.56
0   data    data    data    data
1   data    data    data    data
2   data    data    data    data
3   data    data    data    data
4   data    data    data    data
""")
df = pd.read_csv(data,delim_whitespace=True, index_col=0)

Then use this

df.transpose().reset_index(drop = False).transpose().reset_index(drop = True)

to produce this


           0       1       2       3
0       3.14    3.28    3.42    3.56
1       data    data    data    data
2       data    data    data    data
3       data    data    data    data
4       data    data    data    data
5       data    data    data    data

Not the answer you're looking for? Browse other questions tagged or ask your own question.