#Dataframe数据塞进列表 listbook2 = [] listbook = [] for i in range(book2.iloc[:,0].size): #生成列表 s = book2.iloc[i]['Age'] s = s.strip() listbook.append(s) s = book2.iloc[i]['Sex'] s = s.strip() listbook.append(s) s = book2.iloc[i]['Marital status?'] s = s.strip() listbook.append(s) s = book2.iloc[i]['Education'] s = s.strip() listbook.append(s) s = book2.iloc[i]['Employement'] s = s.strip() listbook.append(s) s = book2.iloc[i]['How many books did you read during last 12months?'] s = s.strip() listbook.append(s) s = book2.iloc[i]['Read any printed books during last 12months?'] s = s.strip() listbook.append(s) s = book2.iloc[i]['Read any audiobooks during last 12months?'] s = s.strip() listbook.append(s) s = book2.iloc[i]['Read any e-books during last 12months?'] s = s.strip() listbook.append(s) s = book2.iloc[i]['Do you happen to read any daily news or newspapers?'] s = s.strip() listbook.append(s) s = book2.iloc[i]['Do you happen to read any magazines or journals?'] s = s.strip() listbook.append(s) listbook2.append(listbook.copy()) listbook.clear()
时间: 2023-06-26 21:09:58 浏览: 119
Python 将DataFrame数据转换成列表 Python源码
这段代码是将一个名为book2的DataFrame数据,转换成了一个二维列表listbook2。其中,每一行数据都被转换成了一个包含多个元素的列表listbook,并且这些列表按顺序存储在listbook2中。
具体来说,这个代码首先定义了两个空列表listbook和listbook2,然后通过循环遍历book2的每一行数据。对于每一行数据,都将其中的每个元素提取出来,并去除两端的空格,然后添加到listbook中。最后,将这个已经填满数据的listbook复制一份添加到listbook2中,并清空listbook以便下一次循环使用。最终,listbook2就成为了一个由多个listbook组成的二维列表,其中每个listbook都代表了一行数据。
阅读全文