简体   繁体   English

将 python 列表转换为 pandas dataframe 时出现问题

[英]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:我正在尝试将其转换为 dataframe 使用:

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:它创建了一个 dataframe 但是它将所有数据堆叠在一行中,如下所示:

                                       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?当我检查列表的长度时,它显示 1. 这到底是什么问题? 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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