pandas將Excel的所有行轉化為列表list

baidu_252253發表於2019-02-20

首先使用np.array()函式把DataFrame轉化為np.ndarray(),再利用tolist()函式把np.ndarray()轉為list,示例程式碼如下:

import numpy as np
import pandas as pd

data_x = pd.read_csv("E:/Tianchi/result/features.csv",usecols=[2,3,4])#pd.dataframe
data_y =  pd.read_csv("E:/Tianchi/result/features.csv",usecols=[5])

train_data = np.array(data_x)#np.ndarray()
train_x_list=train_data.tolist()#list
print(train_x_list)
print(type(train_x_list))
--------------------- 
作者:nudt_qxx 
來源:CSDN 
原文:https://blog.csdn.net/xiangxianghehe/article/details/72615711 
版權宣告:本文為博主原創文章,轉載請附上博文連結!

# ---------------------綜合小例子----------------------------
col_names = food_info.columns.tolist()     #把所有的行轉化成list
print(col_names)
gram_columns = []

for c in col_names:                        #遍歷col_names,找出所有以(g)結尾的位置
    if c.endswith("(g)"):
        gram_columns.append(c)
print(gram_columns)
gram_df = food_info[gram_columns]          #把所有以(g)結尾的列存放到gram_df
print(gram_df.head(3)) 
--------------------- 
作者:資料阿伯 
來源:CSDN 
原文:https://blog.csdn.net/qq_32005671/article/details/61199628 
版權宣告:本文為博主原創文章,轉載請附上博文連結!

相關文章