I am kind of new to programming and it is not my branch, but I need this task to analyze my data in a few days and now don't have enough time to be devoted to study of python. Let's say I have data file that looks like this:
0 2
0.5 8.4
1 354.8
0 41
0.5 2
1 48.8
Now I would need to create from this new data file that would look like this:
0 2 41
0.5 8.4 2
1 354.8 48.8
Whenever there is a zero value in first column it will start write data from the second column to the new column. The sequence of numbers in first column is always repeating. This up here is just a special case, there will be more repeating sequences with 0, 0.5 and 1.
I am kindly asking for some help with this task or for giving me some direction how to solve it in python.
Use pandas. Assuming names on the columns:
df.columns = ['a', 'b']
df
a b
0 0.0 2.0
1 0.5 8.4
2 1.0 354.8
3 0.0 41.0
4 0.5 2.0
5 1.0 48.8
You can use:
df2 = df.groupby(['a'])['b'].apply(list).reset_index() # group by "a", get grouped "b" in list format
and get:
a b
0 0.0 [2.0, 41.0]
1 0.5 [8.4, 2.0]
2 1.0 [354.8, 48.8]
And finally extract the row data:
Row_list =[]
for index, rows in df2.iterrows():
my_list =[rows.a, rows.b]
my_list= str(my_list).replace('[','').replace(']','')
Row_list.append(my_list)
print(Row_list)
['0.0, 2.0, 41.0', '0.5, 8.4, 2.0', '1.0, 354.8, 48.8']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With