Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Array manipulation
#8
Ok, so I've managed to get it working, although it might not be the most elegant solution. I build the new dataframe based on data extracted from the old dataframe. Innitially I got a lot of 'nan's in the new dataframe. I traced it to an indexing issue where the data I extract from the old dataframe keep it's index, which causes a mismatch with the new dataframe's index, therefore causing the 'nan'. So before adding the data extracted from the old dataframe, I needed to reindex it to align with the new dataframe's index. See code below. As I'm new to python I welcome comments on how to do this better, more efficiently. For some reason the reindexing takes quite a bit of processing time.

# read csv into dataframe
df = pd.read_csv("MBV2rawdata.csv") #CPID, Date, Value, Class

# I need this in the format Date, CPID#1, CPID#2, ... , CPID#n, Class


cpid = df.CPID.unique()


sf = df.Date[df.CPID == 24021] #Sf is the newly created dataframe

sf = pd.DataFrame(sf)


i=0
for colname in cpid:
    col = pd.DataFrame(df.Value[df.CPID==colname]) #get the column from df   
    for j in range(len(col)):   #have to reindex col to align with sf's index
        col.rename(index = { j + len(col)*i : j}, inplace=True)
    sf.insert(i+1, colname, col, True) #add col to sf
    i+=1


sf['Class']=df.Class[df.CPID == 24021] # add the 'Class' vlaues to sf
Reply


Messages In This Thread
Array manipulation - by 0zMeister - Feb-09-2020, 05:52 AM
RE: Array manipulation - by Larz60+ - Feb-09-2020, 07:25 AM
RE: Array manipulation - by perfringo - Feb-09-2020, 07:56 AM
RE: Array manipulation - by 0zMeister - Feb-09-2020, 08:30 AM
RE: Array manipulation - by jefsummers - Feb-09-2020, 01:42 PM
RE: Array manipulation - by 0zMeister - Feb-09-2020, 07:23 PM
RE: Array manipulation - by baquerik - Feb-09-2020, 08:57 PM
RE: Array manipulation - by 0zMeister - Feb-15-2020, 12:34 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  array manipulation divon 3 1,850 Sep-03-2021, 09:44 AM
Last Post: divon
  Help with array manipulation pberrett 4 2,495 Mar-30-2020, 03:02 AM
Last Post: pberrett
  N-Dim array manipulation in a loop, getting IndexError: too many indices for array cesardepaula 1 4,520 Mar-13-2019, 01:39 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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