Posts: 4
Threads: 1
Joined: Dec 2021
Hi everyone, I am new to this and maybe someone help me with this super simple question.
I have a table with this information
_c0 _c4
0 b
0 g
0 f
1 a
1 f
1 c
2 f
2 e
2 a
2 c
and I need to move this way. I mean, How can group this duplicate information in a list? :
_c0 lista
0 b,f,g
1 a,c,f
2 a,c,e,f
Thanks for support me!!!
Jalena.
Posts: 12,038
Threads: 487
Joined: Sep 2016
Please show how the data is arranged in python.
for example: co = ['0 b', '0 g', '0 f', '1 a', '1 f', '1 c', '2 f', '2 e', '2 a', '2 c']
or some other format.
Posts: 4
Threads: 1
Joined: Dec 2021
Dec-28-2021, 02:38 AM
(This post was last modified: Dec-28-2021, 06:06 PM by Larz60+.
Edit Reason: Fixed bbcode tags
)
Hi,
It is dataframe and I am importing with pandas. Look
Output: %%writefile tbl1.tsv
_c0 _c4
0 b
0 g
0 f
1 a
1 f
1 c
2 f
2 e
2 a
2 c
import pandas as pd
tbl1 = pd.read_csv('tbl1.tsv', sep='\t')
tbl1.dtypes
tbl1 Thanks!
Posts: 12,038
Threads: 487
Joined: Sep 2016
I don't have a copy of tabl1.tsv
Help me to give help please.
Posts: 4
Threads: 1
Joined: Dec 2021
I attach it the table
thanks!
Attached Files
tbl1.csv (Size: 62 bytes / Downloads: 106)
Posts: 7,324
Threads: 123
Joined: Sep 2016
Something like this.
import pandas as pd
from io import StringIO
data = StringIO("""\
_c0,_c4
0,b
0,g
0,f
1,a
1,f
1,c
2,f
2,e
2,a
2,c
""")
df = pd.read_csv(data, sep=",") >>> df = df.groupby('_c0')
>>> grouped_list = df["_c4"].apply(list)
>>> grouped_list
_c0
0 [b, g, f]
1 [a, f, c]
2 [f, e, a, c]
Name: _c4, dtype: object
>>>
>>> df = grouped_list.reset_index()
>>> df
_c0 _c4
0 0 [b, g, f]
1 1 [a, f, c]
2 2 [f, e, a, c]
Posts: 379
Threads: 2
Joined: Jan 2021
Dec-28-2021, 10:38 PM
(This post was last modified: Dec-28-2021, 10:38 PM by BashBedlam.)
Here's one way to go about it:
array = ['_c0 lista']
with open ('tbl1.csv', 'r') as in_file :
in_file.readline ()
for line in in_file :
for index in range (len (array)) :
if line [0] == array [index][0] :
array [index] = array [index] + f',{line [2]}'
break
else :
array.append (line.strip ())
with open ('tbl2.csv', 'w') as out_file :
for line in array :
out_file.write (f'{line}\n') Output: @LapTop:~$ cat tbl2.csv
_c0 lista
0,b,g,f
1,a,f,c
2,f,e,a,c
Posts: 4
Threads: 1
Joined: Dec 2021
Thanks snippsat!! for helping me
|