Python Forum
Transposing a dataframe without creating NaN values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Transposing a dataframe without creating NaN values
#1
Hi! I have the data like this in a .csv file:
index data
1 10
2 20
3 30
4 40
5 50
6 10
2 20
3 30
4 40
5 50
6. 60

And I would need them to be read like this in Python:
1 2 3 4 5 6
10 20 30 40 50 60
10 20 30 40 50 60

But it transposes the values and NaN appears between them as if it were 0 in a matrix:
1 2 3 4 5 6
10 NaN NaN NaN NaN. NaN
NaN 20 NaN NaN NaN NaN
NaN NaN 30 NaN NaN NaN
NaN NaN NaN 40 NaN NaN
NaN NaN NaN NaN 50 NaN
NaN NaN NaN NaN NaN 60
Reply
#2
It important to be precise. If your data is in a file as provided then you could not get desired output. File has 11 rows.

Other than that - please provide your code.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Indeed, not sure what the issue is. Please post your code. I did it as:
import pandas as pd

from_csv_data = [[1,10],[2,20],[3,30],[4,40],[5,50],[6,10],[2,20],[3,30],[4,40],[5,50],[6,60]]
df = pd.DataFrame(from_csv_data)
df0 = df.set_index(0)
df0
Output:
1 0 1 10 2 20 3 30 4 40 5 50 6 10 2 20 3 30 4 40 5 50 6 60
df1=df0.transpose()
df1
Output:
1 2 3 4 5 6 2 3 4 5 6 1 10 20 30 40 50 10 20 30 40 50 60
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,509 Jul-27-2022, 08:50 PM
Last Post: rob101
  Creating a numpy array from specific values of a spreadsheet column JulianZ 0 1,135 Apr-19-2022, 07:36 AM
Last Post: JulianZ
  List of dataframe values beginning with x,y or z glidecode 3 1,950 Nov-08-2021, 10:16 PM
Last Post: glidecode
  How to sort values descending from a row in a dataframe using python sankarachari 1 1,427 Aug-16-2021, 08:55 AM
Last Post: jamesaarr
  How to get value in Dataframe given row & column values? moonlight 1 2,437 Apr-26-2021, 09:30 PM
Last Post: Larz60+
  Xlsxwriter: Create Multiple Sheets Based on Dataframe's Sorted Values KMV 2 3,514 Mar-09-2021, 12:24 PM
Last Post: KMV
  Dataframe extract key values danipyth 0 1,671 Feb-07-2021, 03:52 PM
Last Post: danipyth
  Assigning Column nunique values to another DataFrame column Pythonito 1 1,889 Jun-26-2020, 06:52 AM
Last Post: hussainmujtaba
  Do Calculation between Rows based on Column values - Pandas Dataframe ahmedwaqas92 0 2,156 Jan-28-2020, 07:06 AM
Last Post: ahmedwaqas92
  Creating a new DataFrame from another DataFrame column Ilangos 1 2,074 Jun-05-2019, 09:47 AM
Last Post: Ilangos

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020