从 excel 文件列中读取列表列表并将其存储在 python 列表中
Reading a list of lists from an excel file column and storing it in a python list
我知道这个问题的某些部分可能很简单,但我是这方面的初学者,非常感谢最简单的解决方案:我有一个 excel(.xlsx 文件),其中一列有它的每个单元格都有一个数字列表列表(数字 space 分隔,每个列表的末尾甚至还有一个 space )。因此,该列看起来像这样:
ColumnHeader
[[[9 9 9 9 9 13 ][11 11 11 11 11 11 ][11 11 11 11 11 11 ][9 9 9 9 9 9 ]
[[[9 9 9 9 9 9 ][9 9 9 9 9 9 ]]]
[[[9 9 9 9 ][14 14 14 14 ][13 13 13 13 ]]]
请注意每个列表有不同数量的列表。另请注意,列表的每个列表前后分别有一个额外的 [ 和 ]。
我想做的是理想地读取 python 中的整个 xlsx 文件(记住文件中还有其他列只有数字),将其存储在 pandas数据框,但将上面的这一列存储为列表列表。因此,如果我稍后打印此专栏,我会得到类似下面的内容(如果转换为列表,该系列将是列表列表的列表:
ColumnHeader
[[9,9,9,9,9,13],[11,11,11,11,11,11],[11,11,11,11,11,11],[9,9,9,9,9,9]]
[[9,9,9,9,9,9],[9,9,9,9,9,9]]
[[9,9,9,9],[14,14,14,14],[13,13,13,13]]
如果我只是直接将 xlsx 文件读入 pandas 数据帧,它显然会将此列读取为文本,这不是我想要的。
如有任何帮助,我们将不胜感激。
阿里
我建议您将受控列作为字符串加载,然后使用 this functionality 将其转换为嵌套列表。定义一个接受字符串和 returns 列表的函数:
import pandas as pd
import ast
# Load some test data
df = pd.DataFrame({'fake_list' : ['[[[9 9 9 9 9 13 ][11 11 11 11 11 11 ][11 11 11 11 11 11 ][9 9 9 9 9 9 ]]]',
'[[[9 9 9 9 9 9 ][9 9 9 9 9 9 ]]] ',
'[[[9 9 9 9 ][14 14 14 14 ][13 13 13 13 ]]]'],
'a': [1,2,3],
'b': [4,5,6]})
def fix_list(s):
s1 = s.strip() #strip white space at the edge of the string
s1 = s1[1:-1] # remove edge parenthesis
s1 = s1.replace(' ',',').replace('][', '],[') # make some replacements so that it looks like a nested list
return ast.literal_eval(s1) # transform string to a nested list
然后将函数应用于需要转换的列:
df['true_list'] = df['fake_list'].apply(fix_list)
print df.true_list[0]
# [[9, 9, 9, 9, 9, 13], [11, 11, 11, 11, 11, 11], [11, 11, 11, 11, 11, 11], [9, 9, 9, 9, 9, 9]]
或者,您可以在使用 converters
读取 excel 的同时转换有罪的列:
df = pd.read_excel('file.xlsx', converters = {'fake_list':fix_list()}
不用 panda 也可以,只需要使用内置的 csv 库
from csv import reader
# read csv file as a list of lists
with open('students.csv', 'r') as read_obj:
# pass the file object to reader() to get the reader object
csv_reader = reader(read_obj)
# Pass reader object to list() to get a list of lists
list_of_rows = list(csv_reader)
print(list_of_rows)
如果你想排除第一行,使用.pop函数
list_of_rows.pop(0)
我知道这个问题的某些部分可能很简单,但我是这方面的初学者,非常感谢最简单的解决方案:我有一个 excel(.xlsx 文件),其中一列有它的每个单元格都有一个数字列表列表(数字 space 分隔,每个列表的末尾甚至还有一个 space )。因此,该列看起来像这样:
ColumnHeader
[[[9 9 9 9 9 13 ][11 11 11 11 11 11 ][11 11 11 11 11 11 ][9 9 9 9 9 9 ]
[[[9 9 9 9 9 9 ][9 9 9 9 9 9 ]]]
[[[9 9 9 9 ][14 14 14 14 ][13 13 13 13 ]]]
请注意每个列表有不同数量的列表。另请注意,列表的每个列表前后分别有一个额外的 [ 和 ]。
我想做的是理想地读取 python 中的整个 xlsx 文件(记住文件中还有其他列只有数字),将其存储在 pandas数据框,但将上面的这一列存储为列表列表。因此,如果我稍后打印此专栏,我会得到类似下面的内容(如果转换为列表,该系列将是列表列表的列表:
ColumnHeader
[[9,9,9,9,9,13],[11,11,11,11,11,11],[11,11,11,11,11,11],[9,9,9,9,9,9]]
[[9,9,9,9,9,9],[9,9,9,9,9,9]]
[[9,9,9,9],[14,14,14,14],[13,13,13,13]]
如果我只是直接将 xlsx 文件读入 pandas 数据帧,它显然会将此列读取为文本,这不是我想要的。
如有任何帮助,我们将不胜感激。
阿里
我建议您将受控列作为字符串加载,然后使用 this functionality 将其转换为嵌套列表。定义一个接受字符串和 returns 列表的函数:
import pandas as pd
import ast
# Load some test data
df = pd.DataFrame({'fake_list' : ['[[[9 9 9 9 9 13 ][11 11 11 11 11 11 ][11 11 11 11 11 11 ][9 9 9 9 9 9 ]]]',
'[[[9 9 9 9 9 9 ][9 9 9 9 9 9 ]]] ',
'[[[9 9 9 9 ][14 14 14 14 ][13 13 13 13 ]]]'],
'a': [1,2,3],
'b': [4,5,6]})
def fix_list(s):
s1 = s.strip() #strip white space at the edge of the string
s1 = s1[1:-1] # remove edge parenthesis
s1 = s1.replace(' ',',').replace('][', '],[') # make some replacements so that it looks like a nested list
return ast.literal_eval(s1) # transform string to a nested list
然后将函数应用于需要转换的列:
df['true_list'] = df['fake_list'].apply(fix_list)
print df.true_list[0]
# [[9, 9, 9, 9, 9, 13], [11, 11, 11, 11, 11, 11], [11, 11, 11, 11, 11, 11], [9, 9, 9, 9, 9, 9]]
或者,您可以在使用 converters
读取 excel 的同时转换有罪的列:
df = pd.read_excel('file.xlsx', converters = {'fake_list':fix_list()}
不用 panda 也可以,只需要使用内置的 csv 库
from csv import reader
# read csv file as a list of lists
with open('students.csv', 'r') as read_obj:
# pass the file object to reader() to get the reader object
csv_reader = reader(read_obj)
# Pass reader object to list() to get a list of lists
list_of_rows = list(csv_reader)
print(list_of_rows)
如果你想排除第一行,使用.pop函数
list_of_rows.pop(0)