將python列表轉換為pandas數據框的問題

technophile_3

我有一個這樣的清單:

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']

我正在嘗試使用以下方法將其轉換為數據幀:

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

它創建了一個數據幀,但是它將所有數據堆疊在一行中,如下所示:

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

當我檢查列表的長度時,它顯示 1. 這到底是什麼問題?難道我做錯了什麼?請幫忙

阿里·奧斯多普

您的列表是一個字符串。它應該更像這樣:

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']

然後它會起作用。


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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章