How can split be used for things that are slightly different in form (?) from each other using split, for, etc.?

Asked 2 years ago, Updated 2 years ago, 104 views

Hello, I am an introvert who has been practicing split, for, and if moon recently.

df = pd.DataFrame({"idx5":[ "ex1.x.1", 
"ex1.x", 
"ex2.x.2", 
"ex2.x.3", 
"ex1.x.2", 
"ex2.x", 
"ex2.x.4", 
"ex2.x.5", 
"ex1.x.3", 
"ex3.x", 
"ex2.x.6", 
"ex2.x.7", 
"ex1.x.4", 
"ex4.x", 
"ex2.x.8", 
"ex2.x.9", 
"ex1.x.5", 
"ex5.x", 
"ex2.x.10", 
"ex2.x.11", 
"ex1.x.6", 
"ex6.x", 
"ex2.x.12", 
"ex2.x.13", 
"ex1.x.7", 
]})

for i in range(len(df)-1) : 
    df.at[i+1, "idx6"], df.at[i+1, "idx7"], df.at[i+1, "idx8"] = df.at[i+1, "idx5"].split(".")
 ValueError: not enough values to unpack (expected 3, got 2)

The error code above is coming out. Is there a way to split even if the number of " is different? Ultimately, I want to make idx6, idx7, idx8 with idx5 only as below. It doesn't matter if you put any characters in the empty space of idx8.!

idx5idx6idx7idx8
ex1.x.1     ex1 x   1
ex1.x       ex1 x   
ex2.x.2     ex2 x   2
ex2.x.3     ex2 x   3
ex1.x.2     ex1 x   2
ex2.x       ex2 x   
ex2.x.4     ex2 x   4
ex2.x.5     ex2 x   5
ex1.x.3     ex1 x   3
ex3.x       ex3 x   
ex2.x.6     ex2 x   6
ex2.x.7     ex2 x   7
ex1.x.4     ex1 x   4
ex4.x       ex4 x   
ex2.x.8     ex2 x   8
ex2.x.9     ex2 x   9
ex1.x.5     ex1 x   5
ex5.x       ex5 x   
ex2.x.10    ex2 x   10
ex2.x.11    ex2 x   11
ex1.x.6     ex1 x   6
ex6.x       ex6 x   
ex2.x.12    ex2 x   12
ex2.x.13    ex2 x   13
ex1.x.7     ex1 x   7

split python for if문

2022-09-20 15:18

1 Answers

Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import pandas as pd

>>> df = pd.DataFrame({"idx5":[ "ex1.x.1", 
"ex1.x", 
"ex2.x.2", 
"ex2.x.3", 
"ex1.x.2", 
"ex2.x", 
"ex2.x.4", 
"ex2.x.5", 
"ex1.x.3", 
"ex3.x", 
"ex2.x.6", 
"ex2.x.7", 
"ex1.x.4", 
"ex4.x", 
"ex2.x.8", 
"ex2.x.9", 
"ex1.x.5", 
"ex5.x", 
"ex2.x.10", 
"ex2.x.11", 
"ex1.x.6", 
"ex6.x", 
"ex2.x.12", 
"ex2.x.13", 
"ex1.x.7", 
]})
>>> df
        idx5
0    ex1.x.1
1      ex1.x
2    ex2.x.2
3    ex2.x.3
4    ex1.x.2
5      ex2.x
6    ex2.x.4
7    ex2.x.5
8    ex1.x.3
9      ex3.x
10   ex2.x.6
11   ex2.x.7
12   ex1.x.4
13     ex4.x
14   ex2.x.8
15   ex2.x.9
16   ex1.x.5
17     ex5.x
18  ex2.x.10
19  ex2.x.11
20   ex1.x.6
21     ex6.x
22  ex2.x.12
23  ex2.x.13
24   ex1.x.7
>>> df[["idx6", "idx7", "idx9"]] = df["idx5"].str.split(".", 3, expand=True)
>>> df
        idx5 idx6 idx7  idx9
0    ex1.x.1  ex1    x     1
1      ex1.x  ex1    x  None
2    ex2.x.2  ex2    x     2
3    ex2.x.3  ex2    x     3
4    ex1.x.2  ex1    x     2
5      ex2.x  ex2    x  None
6    ex2.x.4  ex2    x     4
7    ex2.x.5  ex2    x     5
8    ex1.x.3  ex1    x     3
9      ex3.x  ex3    x  None
10   ex2.x.6  ex2    x     6
11   ex2.x.7  ex2    x     7
12   ex1.x.4  ex1    x     4
13     ex4.x  ex4    x  None
14   ex2.x.8  ex2    x     8
15   ex2.x.9  ex2    x     9
16   ex1.x.5  ex1    x     5
17     ex5.x  ex5    x  None
18  ex2.x.10  ex2    x    10
19  ex2.x.11  ex2    x    11
20   ex1.x.6  ex1    x     6
21     ex6.x  ex6    x  None
22  ex2.x.12  ex2    x    12
23  ex2.x.13  ex2    x    13
24   ex1.x.7  ex1    x     7

>>> df = df.fillna("-")
>>> df
        idx5 idx6 idx7 idx9
0    ex1.x.1  ex1    x    1
1      ex1.x  ex1    x    -
2    ex2.x.2  ex2    x    2
3    ex2.x.3  ex2    x    3
4    ex1.x.2  ex1    x    2
5      ex2.x  ex2    x    -
6    ex2.x.4  ex2    x    4
7    ex2.x.5  ex2    x    5
8    ex1.x.3  ex1    x    3
9      ex3.x  ex3    x    -
10   ex2.x.6  ex2    x    6
11   ex2.x.7  ex2    x    7
12   ex1.x.4  ex1    x    4
13     ex4.x  ex4    x    -
14   ex2.x.8  ex2    x    8
15   ex2.x.9  ex2    x    9
16   ex1.x.5  ex1    x    5
17     ex5.x  ex5    x    -
18  ex2.x.10  ex2    x   10
19  ex2.x.11  ex2    x   11
20   ex1.x.6  ex1    x    6
21     ex6.x  ex6    x    -
22  ex2.x.12  ex2    x   12
23  ex2.x.13  ex2    x   13
24   ex1.x.7  ex1    x    7
>>> 


2022-09-20 15:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.