I want to convert a table, represented as a list of lists, into a Pandas DataFrame. 我想將表示爲列表列表的錶轉換爲Pandas DataFrame。 As an extremely simplified example: 做爲一個極其簡化的示例: app
a = [['a', '1.2', '4.2'], ['b', '70', '0.03'], ['x', '5', '0']] df = pd.DataFrame(a)
What is the best way to convert the columns to the appropriate types, in this case columns 2 and 3 into floats? 將列轉換爲適當類型的最佳方法是什麼,在這種狀況下,將列2和3轉換爲浮點數? Is there a way to specify the types while converting to DataFrame? 有沒有一種方法能夠在轉換爲DataFrame時指定類型? Or is it better to create the DataFrame first and then loop through the columns to change the type for each column? 仍是先建立DataFrame而後遍歷各列以更改各列的類型更好? Ideally I would like to do this in a dynamic way because there can be hundreds of columns and I don't want to specify exactly which columns are of which type. 理想狀況下,我想以動態方式執行此操做,由於能夠有數百個列,而且我不想確切指定哪些列屬於哪一種類型。 All I can guarantee is that each columns contains values of the same type. 我能夠保證的是,每一列都包含相同類型的值。 oop