Reputation: 669
As a result of some operations, I am getting a dataframe that looks like this:
0 1
0 (aut, aut) (1.0, 0.0)
1 (isr, pcn) (0.0621031946211, 0.0840317734128)
2 (wlf, gum) (0.00971778368827, 0.787082275372)
3 (lka, are) (0.184325574632, 2.37291167033e-07)
4 (mmr, brb) (-0.00659784629805, 0.854498462056)
5 (umi, mar) (0.136002437743, 0.000146047773528)
6 (rwa, arm) (0.143873473167, 5.82638804266e-05)
But I need to split this dataframe in something that looks like this:
iso_a iso_b value_1 value_2
0 aut aut 1.0 0.0
1 isr pcn 0.062103194621 0.0840317734128
2 wlf gum 0.009717783688 0.787082275372
3 lka are 0.184325574632 2.37291167033e-07
4 mmr brb -0.006597846298 0.854498462056
5 umi mar 0.136002437743 0.000146047773528
6 rwa arm 0.143873473167 5.82638804266e-05
Upvotes: 0
Views: 54
Reputation: 75
I might:
def x(col):
return col[0]
df['ios_a'] = df[0].apply(x)
df['value_1'] = df[1].apply(x)
def y(col):
return col[1]
df['ios_b'] = df[0].apply(y)
df['value_2'] = df[1].apply(y)
And then you can delete your first two columns if you like.
del df[0]
del df[1]
This is a little clumsy (not DRY) but does the job. def x():
takes the column (either column df[0]
or df[1]
) and then returns the first part of the tuple in each row, putting it in the new assigned column (e.g. df['iso_a']
)
Then def y():
does the same, but this time returns the second part of each tuple. Does that make sense? Also, this is assuming you're using Pandas dataframe.
Upvotes: 1
Reputation: 84
I am not sure if this is a input file or a multidimensional array. Let's say your input data frame is a multidimensional array where each element has another array with two elements.
def getListOfDictionaries(dataFrame):
newList = list()
for row in dataFrame:
newList.append({'iso_a': row[0][0],
'iso_b': row[0][1],
'value_1': row[1][0],
'value_2': row[1][1]})
return newList
As I said, I don't know in what format we can expect the input data
Upvotes: 0
Reputation: 42329
Since you give very (no) details on what format you need the input data read, here's a rudimentary but simple way:
ls = []
with open('del.txt', 'r') as f:
for line in f:
ls.append(line.replace('(', '').replace(')', '').replace(',', '').split())
for l in ls[1:]:
print(l)
This gives a list with a sub-list for every row, with every element stored as a string:
['0', 'aut', 'aut', '1.0', '0.0']
['1', 'isr', 'pcn', '0.0621031946211', '0.0840317734128']
['2', 'wlf', 'gum', '0.00971778368827', '0.787082275372']
['3', 'lka', 'are', '0.184325574632', '2.37291167033e-07']
['4', 'mmr', 'brb', '-0.00659784629805', '0.854498462056']
['5', 'umi', 'mar', '0.136002437743', '0.000146047773528']
['6', 'rwa', 'arm', '0.143873473167', '5.82638804266e-05']
Here's another way using the translate method, which produces the same result
ls = []
with open('del.txt', 'r') as f:
for line in f:
ls.append(line.translate(None, "(),").split())
Upvotes: 1