0

I have a list like this:

highlights=
['Security Name % to Net Assets* DEBENtURES 0.04, Britannia Industries Ltd. EQUity & RELAtED 96.83, HDFC Bank 6.98, ICICI 4.82, Infosys 4.37, Reliance 4.05, Bajaj Finance 3.82, Housing De
velopment Corpn. 3.23, Grindwell Norton 3.22, SRF Sun Pharmaceutical 2.85, Bharti Airtel 2.82, DLF 2.64, Ultratech Cement 2.62, SKF India 2.45, Crompton Greaves Consumer Electricals 2.42,
 Avenue Supermarts 2.41, Axis ABB 2.35, Titan Co. 2.29, Kotak Mahindra 2.09, Cipla 2.05, Laurus Labs 2.04, Wipro 1.77, Happiest Minds Technologies 1.68, Canara 1.67, Shree 1.63, 1.59, Pid
ilite 1.50, Lombard General Insurance 1.48, Cholamandalam Investment 1.45, Tech 1.35, State of 1.31, Hindustan Unilever 1.30, Vardhman Textiles Larsen Toubro 1.24, Dabur 1.22, Neogen Chem
icals 1.10, Eicher Motors 1.09, Thermax 1.08, TATA Consultancy Services 1.05, Indian Railway Catering Tourism 0.98, Firstsource Solutions 0.97, Nestle 0.86, Asian Paints 0.84, Welspun 0.7
2, IndusInd 0.63, SBI Life 0.50, Deepak Nitrite 0.46, Adani Ports and Special Economic Zone 0.36, Gateway Distriparks 0.33, Bharat Forge 0.22, tREPS on G-Sec or t-Bills 2.81, Cash Receiva
bles 0.32, tOtAL']

and I am trying to convert it into a dataframe using:

df = pd.DataFrame([highlights], columns=['C-Names'])
print(df)

It creates a dataframe however it stacks all data in a single row something like this:

                                       C-Names
0  Security Name % to Net Assets* DEBENtURES 0.04
1  Axis ABB 2.35
2 Lombard General Insurance 1.48
.
.
.

when I check the length of the list it shows 1. what exactly is the issue in this? Am I doing something wrong? Please help

6
  • Looks like you have a list with just one string inside of it. Commented Dec 7, 2021 at 10:57
  • Your list has a single large string item. Commented Dec 7, 2021 at 10:58
  • What format do you want your data to have? Commented Dec 7, 2021 at 10:58
  • Would also be good if you told us how the output is supposed to look like. Commented Dec 7, 2021 at 10:59
  • @Djib2011 in a dataframe Commented Dec 7, 2021 at 11:04

1 Answer 1

1

Your list is one string. It should be more like this:

highlights = ['Security Name % to Net Assets* DEBENtURES 0.04', 'Britannia Industries Ltd. EQUity & RELAtED 96.83', 'HDFC Bank 6.98, ICICI 4.82, Infosys 4.37', 'Reliance 4.05']

Then it would work.


df = pd.DataFrame(highlights)

df
    0
0   Security Name % to Net Assets* DEBENtURES 0.04
1   Britannia Industries Ltd. EQUity & RELAtED 96.83
2   HDFC Bank 6.98, ICICI 4.82, Infosys 4.37
3   R|eliance 4.05
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way of making it like this? because I am getting it from somewhere else.
You would need to convert the comma in the string to " , ". So something like highlights = highlights.replace(",", "\',\'")

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.