-1
data = pd.read_json('https://raw.githubusercontent.com/OMS1996/DAV-5400/master/Books.json')
data= data.transpose()
data=data[["Title","Authors","Publisher","Language","Pages"]]
data

This is the result of the code above

        Title               Authors                      Publisher                  Language      Pages
Book1   David Copperfield   Charles Dickens              Wordsworth Editions Ltd    English       768
Book2   C how to program    [Havey Deitel, Paul Deitel]  Prentice Hall              English       912
Book3   12 Rules for life   Jordan Peterson              Penguin                                    English       448

I would like to remove the '[' as well as the ']' from the Author column. I tried .replace(,) it didn't work and I also tried .str.replace it didn't work.

All the similar questions didn't help

Thank you

1
  • it didn’t work Please elaborate. What exactly did you do, did it throw an error, what did the result look like, etc.? The formatting of the DataFrame output is off. Commented Nov 28, 2019 at 4:21

4 Answers 4

1

Try this

df = pd.DataFrame({"auth":["Charles Dickens","[Havey Deitel, Paul Deitel]","Jordan Peterson"]})
df["auth"] = df["auth"].str.replace("[","").str.replace("]","")
Sign up to request clarification or add additional context in comments.

Comments

1

It is the list. Just run this to see the output

data.Authors.to_dict()

Out[68]:
{'Book1': 'Charles Dickens',
 'Book2': ['Havey Deitel', 'Paul Deitel'],
 'Book3': 'Jordan Peterson'}

To convert them to string and strip [], simply map repr and replace []' with ''

s = data.Authors.map(repr).str.replace("[\[\]\']", '')

Out[83]:
Book1              Charles Dickens
Book2    Havey Deitel, Paul Deitel
Book3              Jordan Peterson
Name: Authors, dtype: object

print(s.to_dict())

Out[87]:
{'Book1': 'Charles Dickens',
 'Book2': 'Havey Deitel, Paul Deitel',
 'Book3': 'Jordan Peterson'}

Comments

0

You can try:

data.Authors.apply(lambda x: ', '.join(x) if type(x) == list  else x)

output:

Book1              Charles Dickens
Book2    Havey Deitel, Paul Deitel
Book3              Jordan Peterson
data.Authors = data.Authors.apply(lambda x: ', '.join(x) if type(x) == list  else x) 
                   Title                    Authors                Publisher Language Pages
Book1  David Copperfield            Charles Dickens  Wordsworth Editions Ltd  English   768
Book2   C how to program  Havey Deitel, Paul Deitel            Prentice Hall  English   912
Book3  12 Rules for life            Jordan Peterson                  Penguin  English   448

Comments

0
  1. Try this -

    data['Authors'] = data['Authors'].apply(lambda x: ",".join(x) if isinstance(x, list) else x)
    data['Authors'] = data['Authors'].str.replace(",", " ")
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.