split & concat string from dataframe
From dataframe,
I want to split from (col1) the number before first symbol |
into a
list, the second number after that into b
list and string from(col1), (text1), (text2), (text3) into text
list
col1 | text1 | text2 | text3
1|6|Show | us the | straight way | null
109|2|I | worship | not that | which ye worship
the output that I expected
a = [1, 109]
b = [6, 2]
text = [‘Show us the straight way’, ‘I worship not that which ye worship’]
what is the best way to do this?
python pandas dataframe split concat
add a comment |
From dataframe,
I want to split from (col1) the number before first symbol |
into a
list, the second number after that into b
list and string from(col1), (text1), (text2), (text3) into text
list
col1 | text1 | text2 | text3
1|6|Show | us the | straight way | null
109|2|I | worship | not that | which ye worship
the output that I expected
a = [1, 109]
b = [6, 2]
text = [‘Show us the straight way’, ‘I worship not that which ye worship’]
what is the best way to do this?
python pandas dataframe split concat
add a comment |
From dataframe,
I want to split from (col1) the number before first symbol |
into a
list, the second number after that into b
list and string from(col1), (text1), (text2), (text3) into text
list
col1 | text1 | text2 | text3
1|6|Show | us the | straight way | null
109|2|I | worship | not that | which ye worship
the output that I expected
a = [1, 109]
b = [6, 2]
text = [‘Show us the straight way’, ‘I worship not that which ye worship’]
what is the best way to do this?
python pandas dataframe split concat
From dataframe,
I want to split from (col1) the number before first symbol |
into a
list, the second number after that into b
list and string from(col1), (text1), (text2), (text3) into text
list
col1 | text1 | text2 | text3
1|6|Show | us the | straight way | null
109|2|I | worship | not that | which ye worship
the output that I expected
a = [1, 109]
b = [6, 2]
text = [‘Show us the straight way’, ‘I worship not that which ye worship’]
what is the best way to do this?
python pandas dataframe split concat
python pandas dataframe split concat
edited Nov 26 '18 at 8:49
Rishabh Agarwal
954322
954322
asked Nov 26 '18 at 8:15
RuzannahRuzannah
699
699
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This is straightforward assuming col1 has 3 pipe-separated elements throughout.
a,b,C = zip(*df.col1.str.split('|'))
D = df.drop('col1', 1).agg(lambda x: ' '.join(x.dropna()), axis=1)
c = [c + ' ' + d for c,d in zip(c,D)]
print(a)
('1', '109')
print(b)
('6', '2')
print(c)
['Show us the straight way', 'I worship not that which ye worship']
Note that a
and b
is a collection of strings, you can map them to numeric with
a, b = map(pd.to_numeric, (a,b))
...to get arrays of integers.
To handle the generic case of col1 having any number of values, you will need to
v = df.col1.str.split('|', expand=True)
m = v.applymap(str.isdigit)
a,b,*_ = v[m].T.agg(lambda x: x.dropna().tolist(), axis=1)
print(a)
['1', '109']
print(b)
['6', '2']
C
can be computed similarly:
C = v[~m].agg(lambda x: x.dropna().str.cat(sep=' '), axis=1).tolist()
and then small c
can be computed as before.
cool! thank you!!
– Ruzannah
Nov 26 '18 at 9:27
@Ruzannah Thanks for accepting, please also consider upvoting the answer, tia :-)
– coldspeed
Nov 26 '18 at 23:08
i really appreciate, but sorry for my newbie respon 😂
– Ruzannah
Nov 26 '18 at 23:14
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53477006%2fsplit-concat-string-from-dataframe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is straightforward assuming col1 has 3 pipe-separated elements throughout.
a,b,C = zip(*df.col1.str.split('|'))
D = df.drop('col1', 1).agg(lambda x: ' '.join(x.dropna()), axis=1)
c = [c + ' ' + d for c,d in zip(c,D)]
print(a)
('1', '109')
print(b)
('6', '2')
print(c)
['Show us the straight way', 'I worship not that which ye worship']
Note that a
and b
is a collection of strings, you can map them to numeric with
a, b = map(pd.to_numeric, (a,b))
...to get arrays of integers.
To handle the generic case of col1 having any number of values, you will need to
v = df.col1.str.split('|', expand=True)
m = v.applymap(str.isdigit)
a,b,*_ = v[m].T.agg(lambda x: x.dropna().tolist(), axis=1)
print(a)
['1', '109']
print(b)
['6', '2']
C
can be computed similarly:
C = v[~m].agg(lambda x: x.dropna().str.cat(sep=' '), axis=1).tolist()
and then small c
can be computed as before.
cool! thank you!!
– Ruzannah
Nov 26 '18 at 9:27
@Ruzannah Thanks for accepting, please also consider upvoting the answer, tia :-)
– coldspeed
Nov 26 '18 at 23:08
i really appreciate, but sorry for my newbie respon 😂
– Ruzannah
Nov 26 '18 at 23:14
add a comment |
This is straightforward assuming col1 has 3 pipe-separated elements throughout.
a,b,C = zip(*df.col1.str.split('|'))
D = df.drop('col1', 1).agg(lambda x: ' '.join(x.dropna()), axis=1)
c = [c + ' ' + d for c,d in zip(c,D)]
print(a)
('1', '109')
print(b)
('6', '2')
print(c)
['Show us the straight way', 'I worship not that which ye worship']
Note that a
and b
is a collection of strings, you can map them to numeric with
a, b = map(pd.to_numeric, (a,b))
...to get arrays of integers.
To handle the generic case of col1 having any number of values, you will need to
v = df.col1.str.split('|', expand=True)
m = v.applymap(str.isdigit)
a,b,*_ = v[m].T.agg(lambda x: x.dropna().tolist(), axis=1)
print(a)
['1', '109']
print(b)
['6', '2']
C
can be computed similarly:
C = v[~m].agg(lambda x: x.dropna().str.cat(sep=' '), axis=1).tolist()
and then small c
can be computed as before.
cool! thank you!!
– Ruzannah
Nov 26 '18 at 9:27
@Ruzannah Thanks for accepting, please also consider upvoting the answer, tia :-)
– coldspeed
Nov 26 '18 at 23:08
i really appreciate, but sorry for my newbie respon 😂
– Ruzannah
Nov 26 '18 at 23:14
add a comment |
This is straightforward assuming col1 has 3 pipe-separated elements throughout.
a,b,C = zip(*df.col1.str.split('|'))
D = df.drop('col1', 1).agg(lambda x: ' '.join(x.dropna()), axis=1)
c = [c + ' ' + d for c,d in zip(c,D)]
print(a)
('1', '109')
print(b)
('6', '2')
print(c)
['Show us the straight way', 'I worship not that which ye worship']
Note that a
and b
is a collection of strings, you can map them to numeric with
a, b = map(pd.to_numeric, (a,b))
...to get arrays of integers.
To handle the generic case of col1 having any number of values, you will need to
v = df.col1.str.split('|', expand=True)
m = v.applymap(str.isdigit)
a,b,*_ = v[m].T.agg(lambda x: x.dropna().tolist(), axis=1)
print(a)
['1', '109']
print(b)
['6', '2']
C
can be computed similarly:
C = v[~m].agg(lambda x: x.dropna().str.cat(sep=' '), axis=1).tolist()
and then small c
can be computed as before.
This is straightforward assuming col1 has 3 pipe-separated elements throughout.
a,b,C = zip(*df.col1.str.split('|'))
D = df.drop('col1', 1).agg(lambda x: ' '.join(x.dropna()), axis=1)
c = [c + ' ' + d for c,d in zip(c,D)]
print(a)
('1', '109')
print(b)
('6', '2')
print(c)
['Show us the straight way', 'I worship not that which ye worship']
Note that a
and b
is a collection of strings, you can map them to numeric with
a, b = map(pd.to_numeric, (a,b))
...to get arrays of integers.
To handle the generic case of col1 having any number of values, you will need to
v = df.col1.str.split('|', expand=True)
m = v.applymap(str.isdigit)
a,b,*_ = v[m].T.agg(lambda x: x.dropna().tolist(), axis=1)
print(a)
['1', '109']
print(b)
['6', '2']
C
can be computed similarly:
C = v[~m].agg(lambda x: x.dropna().str.cat(sep=' '), axis=1).tolist()
and then small c
can be computed as before.
edited Nov 26 '18 at 8:26
answered Nov 26 '18 at 8:19
coldspeedcoldspeed
139k24152239
139k24152239
cool! thank you!!
– Ruzannah
Nov 26 '18 at 9:27
@Ruzannah Thanks for accepting, please also consider upvoting the answer, tia :-)
– coldspeed
Nov 26 '18 at 23:08
i really appreciate, but sorry for my newbie respon 😂
– Ruzannah
Nov 26 '18 at 23:14
add a comment |
cool! thank you!!
– Ruzannah
Nov 26 '18 at 9:27
@Ruzannah Thanks for accepting, please also consider upvoting the answer, tia :-)
– coldspeed
Nov 26 '18 at 23:08
i really appreciate, but sorry for my newbie respon 😂
– Ruzannah
Nov 26 '18 at 23:14
cool! thank you!!
– Ruzannah
Nov 26 '18 at 9:27
cool! thank you!!
– Ruzannah
Nov 26 '18 at 9:27
@Ruzannah Thanks for accepting, please also consider upvoting the answer, tia :-)
– coldspeed
Nov 26 '18 at 23:08
@Ruzannah Thanks for accepting, please also consider upvoting the answer, tia :-)
– coldspeed
Nov 26 '18 at 23:08
i really appreciate, but sorry for my newbie respon 😂
– Ruzannah
Nov 26 '18 at 23:14
i really appreciate, but sorry for my newbie respon 😂
– Ruzannah
Nov 26 '18 at 23:14
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53477006%2fsplit-concat-string-from-dataframe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown