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










0















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















0















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













0












0








0








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












2 Answers
2






active

oldest

votes


















1














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





share|improve this answer























  • 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


















1














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





share|improve this answer























  • 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










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
);



);













draft saved

draft discarded


















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









1














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





share|improve this answer























  • 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















1














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





share|improve this answer























  • 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













1












1








1







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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













1














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





share|improve this answer























  • 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















1














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





share|improve this answer























  • 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













1












1








1







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved