简体   繁体   中英

Issue in transforming python list to pandas dataframe

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM