split the text by comma and append rows pandas2019 Community Moderator ElectionAdd one row to pandas DataFrameSelecting multiple columns in a pandas dataframeRenaming columns in pandasSplit (explode) pandas dataframe string entry to separate rowsDelete column from pandas DataFrame by column name“Large data” work flows using pandasHow do I get the row count of a Pandas dataframe?How to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasSplitting and copying csv fields in pandas
Rejected in 4th interview round citing insufficient years of experience
Force user to remove USB token
Do f-stop and exposure time perfectly cancel?
What Happens when Passenger Refuses to Fly Boeing 737 Max?
Is "history" a male-biased word ("his+story")?
Time dilation for a moving electronic clock
The meaning of the "at the of"
Prove that the total distance is minimised (when travelling across the longest path)
Make a transparent 448*448 image
Replacing Windows 7 security updates with anti-virus?
What does it mean when multiple 々 marks follow a 、?
Why don't MCU characters ever seem to have language issues?
When were linguistics departments first established
Is having access to past exams cheating and, if yes, could it be proven just by a good grade?
Can infringement of a trademark be pursued for using a company's name in a sentence?
If the Captain's screens are out, does he switch seats with the co-pilot?
Is it ok to include an epilogue dedicated to colleagues who passed away in the end of the manuscript?
Provisioning profile doesn't include the application-identifier and keychain-access-groups entitlements
Is going from continuous data to categorical always wrong?
Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?
Can you reject a postdoc offer after the PI has paid a large sum for flights/accommodation for your visit?
Should QA ask requirements to developers?
"However" used in a conditional clause?
What happens with multiple copies of Humility and Glorious Anthem on the battlefield?
split the text by comma and append rows pandas
2019 Community Moderator ElectionAdd one row to pandas DataFrameSelecting multiple columns in a pandas dataframeRenaming columns in pandasSplit (explode) pandas dataframe string entry to separate rowsDelete column from pandas DataFrame by column name“Large data” work flows using pandasHow do I get the row count of a Pandas dataframe?How to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasSplitting and copying csv fields in pandas
I have a dataframe a like below:
df = pd.DataFrame(improve this question
add a comment |
I have a dataframe a like below:
df = pd.DataFrame(improve this question
1
Is that dict or just string ?
– Wen-Ben
Mar 6 at 17:26
its string only
– Kumar AK
Mar 6 at 17:28
Is the first string correct? I think is missing a "".
– Daniel Mesejo
Mar 6 at 17:38
add a comment |
I have a dataframe a like below:
df = pd.DataFrame(
1
Is that dict or just string ?
– Wen-Ben
Mar 6 at 17:26
its string only
– Kumar AK
Mar 6 at 17:28
Is the first string correct? I think is missing a "".
– Daniel Mesejo
Mar 6 at 17:38
1
1
Is that dict or just string ?
– Wen-Ben
Mar 6 at 17:26
Is that dict or just string ?
– Wen-Ben
Mar 6 at 17:26
its string only
– Kumar AK
Mar 6 at 17:28
its string only
– Kumar AK
Mar 6 at 17:28
Is the first string correct? I think is missing a "}".
– Daniel Mesejo
Mar 6 at 17:38
Is the first string correct? I think is missing a "}".
– Daniel Mesejo
Mar 6 at 17:38
add a comment |
2 Answers
2
active
oldest
votes
May be this:
import ast
df[['Text1','Text2']]=df.pop('Text').str.split(":",expand=True)
df.Text2=df.Text2.replace("}","",regex=True).apply(ast.literal_eval)
df.Text1=df.Text1.replace("W",'',regex=True)
s=pd.DataFrame('B':np.concatenate(df.Text2.values),index=df.index.repeat(df.Text2.str.len()))
df.join(s).drop('Text2',1).rename(columns='B':'Text2')
Output
User Text1 Text2
0 101 y 8
0 101 y 8 plus
1 102 x 7
2 103 x 7
2 103 x 7+
2 103 x 7++
3 104 x 7
Thanks but the code is. It dynamic. Suppose we have 3 or 4 values.it will throw error. It only works 2 vlaues.
– Kumar AK
Mar 7 at 6:43
add a comment |
Assuming the first dictionary is missing the closing bracket (}
). You could use ast.literal_eval:
import ast
import pandas as pd
df = pd.DataFrame('User': ['101', '102', '103', '104'],
'Text': [""""y":["8","8 plus"]""", """"x":["7"]""", """"x":["7","7+","7++"]""",
""""x":["7"]"""])
# convert to dictionary and drop the text column
dictionaries = df.assign(D=df.Text.apply(ast.literal_eval)).drop('Text', axis=1)
# convert each row to multiple ones (given by the values of each dictionary)
tuples = [(u, k, v) for u, r in dictionaries.values for k, vs in r.items() for v in vs]
result = pd.DataFrame(tuples, columns=['User', 'Text1', 'Text2'])
print(result)
Output
User Text1 Text2
0 101 y 8
1 101 y 8 plus
2 102 x 7
3 103 x 7
4 103 x 7+
5 103 x 7++
6 104 x 7
Thanks but it's a string not dictionary.
– Kumar AK
Mar 7 at 6:43
@KumarAK Is a string representing a dictionary right?
– Daniel Mesejo
Mar 7 at 10:01
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%2f55028882%2fsplit-the-text-by-comma-and-append-rows-pandas%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
May be this:
import ast
df[['Text1','Text2']]=df.pop('Text').str.split(":",expand=True)
df.Text2=df.Text2.replace("}","",regex=True).apply(ast.literal_eval)
df.Text1=df.Text1.replace("W",'',regex=True)
s=pd.DataFrame('B':np.concatenate(df.Text2.values),index=df.index.repeat(df.Text2.str.len()))
df.join(s).drop('Text2',1).rename(columns='B':'Text2')
Output
User Text1 Text2
0 101 y 8
0 101 y 8 plus
1 102 x 7
2 103 x 7
2 103 x 7+
2 103 x 7++
3 104 x 7
Thanks but the code is. It dynamic. Suppose we have 3 or 4 values.it will throw error. It only works 2 vlaues.
– Kumar AK
Mar 7 at 6:43
add a comment |
May be this:
import ast
df[['Text1','Text2']]=df.pop('Text').str.split(":",expand=True)
df.Text2=df.Text2.replace("}","",regex=True).apply(ast.literal_eval)
df.Text1=df.Text1.replace("W",'',regex=True)
s=pd.DataFrame('B':np.concatenate(df.Text2.values),index=df.index.repeat(df.Text2.str.len()))
df.join(s).drop('Text2',1).rename(columns='B':'Text2')
Output
User Text1 Text2
0 101 y 8
0 101 y 8 plus
1 102 x 7
2 103 x 7
2 103 x 7+
2 103 x 7++
3 104 x 7
Thanks but the code is. It dynamic. Suppose we have 3 or 4 values.it will throw error. It only works 2 vlaues.
– Kumar AK
Mar 7 at 6:43
add a comment |
May be this:
import ast
df[['Text1','Text2']]=df.pop('Text').str.split(":",expand=True)
df.Text2=df.Text2.replace("}","",regex=True).apply(ast.literal_eval)
df.Text1=df.Text1.replace("W",'',regex=True)
s=pd.DataFrame('B':np.concatenate(df.Text2.values),index=df.index.repeat(df.Text2.str.len()))
df.join(s).drop('Text2',1).rename(columns='B':'Text2')
Output
User Text1 Text2
0 101 y 8
0 101 y 8 plus
1 102 x 7
2 103 x 7
2 103 x 7+
2 103 x 7++
3 104 x 7
May be this:
import ast
df[['Text1','Text2']]=df.pop('Text').str.split(":",expand=True)
df.Text2=df.Text2.replace("}","",regex=True).apply(ast.literal_eval)
df.Text1=df.Text1.replace("W",'',regex=True)
s=pd.DataFrame('B':np.concatenate(df.Text2.values),index=df.index.repeat(df.Text2.str.len()))
df.join(s).drop('Text2',1).rename(columns='B':'Text2')
Output
User Text1 Text2
0 101 y 8
0 101 y 8 plus
1 102 x 7
2 103 x 7
2 103 x 7+
2 103 x 7++
3 104 x 7
answered Mar 6 at 17:36
anky_91anky_91
8,1122721
8,1122721
Thanks but the code is. It dynamic. Suppose we have 3 or 4 values.it will throw error. It only works 2 vlaues.
– Kumar AK
Mar 7 at 6:43
add a comment |
Thanks but the code is. It dynamic. Suppose we have 3 or 4 values.it will throw error. It only works 2 vlaues.
– Kumar AK
Mar 7 at 6:43
Thanks but the code is. It dynamic. Suppose we have 3 or 4 values.it will throw error. It only works 2 vlaues.
– Kumar AK
Mar 7 at 6:43
Thanks but the code is. It dynamic. Suppose we have 3 or 4 values.it will throw error. It only works 2 vlaues.
– Kumar AK
Mar 7 at 6:43
add a comment |
Assuming the first dictionary is missing the closing bracket (}
). You could use ast.literal_eval:
import ast
import pandas as pd
df = pd.DataFrame('User': ['101', '102', '103', '104'],
'Text': [""""y":["8","8 plus"]""", """"x":["7"]""", """"x":["7","7+","7++"]""",
""""x":["7"]"""])
# convert to dictionary and drop the text column
dictionaries = df.assign(D=df.Text.apply(ast.literal_eval)).drop('Text', axis=1)
# convert each row to multiple ones (given by the values of each dictionary)
tuples = [(u, k, v) for u, r in dictionaries.values for k, vs in r.items() for v in vs]
result = pd.DataFrame(tuples, columns=['User', 'Text1', 'Text2'])
print(result)
Output
User Text1 Text2
0 101 y 8
1 101 y 8 plus
2 102 x 7
3 103 x 7
4 103 x 7+
5 103 x 7++
6 104 x 7
Thanks but it's a string not dictionary.
– Kumar AK
Mar 7 at 6:43
@KumarAK Is a string representing a dictionary right?
– Daniel Mesejo
Mar 7 at 10:01
add a comment |
Assuming the first dictionary is missing the closing bracket (}
). You could use ast.literal_eval:
import ast
import pandas as pd
df = pd.DataFrame('User': ['101', '102', '103', '104'],
'Text': [""""y":["8","8 plus"]""", """"x":["7"]""", """"x":["7","7+","7++"]""",
""""x":["7"]"""])
# convert to dictionary and drop the text column
dictionaries = df.assign(D=df.Text.apply(ast.literal_eval)).drop('Text', axis=1)
# convert each row to multiple ones (given by the values of each dictionary)
tuples = [(u, k, v) for u, r in dictionaries.values for k, vs in r.items() for v in vs]
result = pd.DataFrame(tuples, columns=['User', 'Text1', 'Text2'])
print(result)
Output
User Text1 Text2
0 101 y 8
1 101 y 8 plus
2 102 x 7
3 103 x 7
4 103 x 7+
5 103 x 7++
6 104 x 7
Thanks but it's a string not dictionary.
– Kumar AK
Mar 7 at 6:43
@KumarAK Is a string representing a dictionary right?
– Daniel Mesejo
Mar 7 at 10:01
add a comment |
Assuming the first dictionary is missing the closing bracket (}
). You could use ast.literal_eval:
import ast
import pandas as pd
df = pd.DataFrame('User': ['101', '102', '103', '104'],
'Text': [""""y":["8","8 plus"]""", """"x":["7"]""", """"x":["7","7+","7++"]""",
""""x":["7"]"""])
# convert to dictionary and drop the text column
dictionaries = df.assign(D=df.Text.apply(ast.literal_eval)).drop('Text', axis=1)
# convert each row to multiple ones (given by the values of each dictionary)
tuples = [(u, k, v) for u, r in dictionaries.values for k, vs in r.items() for v in vs]
result = pd.DataFrame(tuples, columns=['User', 'Text1', 'Text2'])
print(result)
Output
User Text1 Text2
0 101 y 8
1 101 y 8 plus
2 102 x 7
3 103 x 7
4 103 x 7+
5 103 x 7++
6 104 x 7
Assuming the first dictionary is missing the closing bracket (}
). You could use ast.literal_eval:
import ast
import pandas as pd
df = pd.DataFrame('User': ['101', '102', '103', '104'],
'Text': [""""y":["8","8 plus"]""", """"x":["7"]""", """"x":["7","7+","7++"]""",
""""x":["7"]"""])
# convert to dictionary and drop the text column
dictionaries = df.assign(D=df.Text.apply(ast.literal_eval)).drop('Text', axis=1)
# convert each row to multiple ones (given by the values of each dictionary)
tuples = [(u, k, v) for u, r in dictionaries.values for k, vs in r.items() for v in vs]
result = pd.DataFrame(tuples, columns=['User', 'Text1', 'Text2'])
print(result)
Output
User Text1 Text2
0 101 y 8
1 101 y 8 plus
2 102 x 7
3 103 x 7
4 103 x 7+
5 103 x 7++
6 104 x 7
answered Mar 6 at 17:51
Daniel MesejoDaniel Mesejo
18.7k21433
18.7k21433
Thanks but it's a string not dictionary.
– Kumar AK
Mar 7 at 6:43
@KumarAK Is a string representing a dictionary right?
– Daniel Mesejo
Mar 7 at 10:01
add a comment |
Thanks but it's a string not dictionary.
– Kumar AK
Mar 7 at 6:43
@KumarAK Is a string representing a dictionary right?
– Daniel Mesejo
Mar 7 at 10:01
Thanks but it's a string not dictionary.
– Kumar AK
Mar 7 at 6:43
Thanks but it's a string not dictionary.
– Kumar AK
Mar 7 at 6:43
@KumarAK Is a string representing a dictionary right?
– Daniel Mesejo
Mar 7 at 10:01
@KumarAK Is a string representing a dictionary right?
– Daniel Mesejo
Mar 7 at 10:01
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%2f55028882%2fsplit-the-text-by-comma-and-append-rows-pandas%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
1
Is that dict or just string ?
– Wen-Ben
Mar 6 at 17:26
its string only
– Kumar AK
Mar 6 at 17:28
Is the first string correct? I think is missing a "".
– Daniel Mesejo
Mar 6 at 17:38