'How to take a Pandas series and split it so it gives me the full name
0
2 ['name:', 'Atlanta', 'GA:', 'Hartsfield-Jackson', 'Atlanta', 'International']
35 ['name:', 'Boston', 'MA:', 'Logan', 'International']
68 ['name:', 'Baltimore', 'MD:', 'Baltimore/Washington', 'International', 'Thurgood', 'Marshall']
101 ['name:', 'Charlotte', 'NC:', 'Charlotte', 'Douglas', 'International']
134 ['name:', 'Washington', 'DC:', 'Ronald', 'Reagan', 'Washington', 'National']
167 ['name:', 'Denver', 'CO:', 'Denver', 'International']
200 ['name:', 'Dallas/Fort', 'Worth', 'TX:', 'Dallas/Fort', 'Worth', 'International']
233 ['name:', 'Detroit', 'MI:', 'Detroit', 'Metro', 'Wayne', 'County']
266 ['name:', 'Newark', 'NJ:', 'Newark', 'Liberty', 'International']
299 ['name:', 'Fort', 'Lauderdale', 'FL:', 'Fort', 'Lauderdale-Hollywood', 'International']
332 ['name:', 'Washington', 'DC:', 'Washington', 'Dulles', 'International']
I have this series above and I want to split each row so that it lists everything like; Atlanta GA: Hartsfield-Jackson Atlanta International. This would be one column in a data frame. Essentially just want to remove the 'name:' at the beginning of each row and then split it so I have the form that I want. I have tried
my_series.str.split("'").str[7].reset_index(drop=True).astype(str)
But my output comes out to
0
0 Hartsfield-Jackson
1 Logan
2 Baltimore/Washington
3 Charlotte
4 Ronald
5 Denver
6 TX:
7 Detroit
8 Newark
9 FL:
10 Washington
Solution 1:[1]
Hey comeyougunners10,
Just as Barmar suggested, you are not dealing with a string; instead, I suspect you are dealing with the list as the value in each cell.
Just do a quick test yourself, and see if the result is a list:
type(df.iloc[index_of_this_column,0])
If you are indeed dealing with lists, then here is how I will approach it.
def pandas_list_str_spliter(input_value):
# we use the join function to turn list into the str
# From here, you should be able to do any transformation you need
# I only removed the "name:" and front & tailing whitespace.
# But you can modify the function to suit your need
return ' '.join(input_value).replace('name:','').strip()
df['Column_Split'] = df['Column_Split'].apply(pandas_list_str_spliter)
I hope this can be helpful.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vae Jiang |
