简体   繁体   English

将具有列名和值的列表转换为数据框并保留值

[英]Convert list which has column names and values to data frame with preserving the values

I have data in form of list which looks like ['abc','bcd','cde',1,2,3]. 我有列表形式的数据,看起来像['abc','bcd','cde',1,2,3]。 My problem is that 'abc','bcd' and 'cde' are column names and 1,2,3 are values of each column. 我的问题是'abc','bcd'和'cde'是列名,而1,2,3是每列的值。

How can I convert list to dataframe preserving the column names and there respective values? 如何将列表转换为数据框,以保留列名和相应的值?

This how my data looks like in list: 这就是我的数据在列表中的样子:

[ sepal_length  sepal_width  petal_length  petal_width  species
0            5.1          3.5           1.4          0.2        0
1            4.9          3.0           1.4          0.2        0
2            4.7          3.2           1.3          0.2        0
3            4.6          3.1           1.5          0.2        0
4            5.0          3.6           1.4          0.2        0
5            5.4          3.9           1.7          0.4        0]

So I dont know if inside it still preserves the data structure or not. 所以我不知道里面是否仍然保留数据结构。

Thank you in advance! 先感谢您!

Try this : 尝试这个 :

l = ['abc','bcd','cde',1,2,3]
keys = l[:len(l)//2]
values = [[e] for e in l[len(l)//2:]]
pd.DataFrame(dict(zip(keys,values)))

Returns 退货

    abc     bcd     cde
0    1       2       3

If you have more than 1 value per column : 如果每列的值大于1:

l = ['abc','bcd','cde',1,2,3,4,5,6]
n_columns = 3
#or
n_columns = len([e for e in l if isinstance(e,str)])
keys = l[:n_columns]
values = [list() for _ in range(n_columns)]
for i,e in enumerate(l[n_columns:]):
    values[i%n_columns].append(e)
pd.DataFrame(dict(zip(keys,values)))

Returns 退货

    abc     bcd     cde
0    1       2       3
1    4       5       6
import pandas as pd
import numpy as np

nb_columns = 3
my_list = ['abc','bcd','cde',1,2,3]

# Extract the data from your list and reshape with the proper form (1 row, X columns)
data = np.reshape(my_list[nb_columns:], (1,nb_columns))

# Create a pandas Dataframe with your data and a list of columns name
my_pandas = pd.DataFrame(data, columns=my_list[:nb_columns])

EDIT : for multiple lines 编辑:多行

my_list = ['abc','bcd','cde',1,2,3,4,5,6]

# Try to count the number of rows present in the list
nb_row = int((len(my_list)-nb_columns)/nb_columns)

# Extract the data from your list and reshape with the proper form (N row, X columns)
data = np.reshape(my_list[nb_columns:], (nb_row, nb_columns))

If you have any other questions. 如果您还有其他问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将具有向量值的熊猫数据框列转换为张量 - Convert pandas data frame column, which has values of vectors, into tensors 转换数据框中具有值的列作为数字列表的列表 - Convert a column in a data frame that has values as a list of lists as numeric 如何转换 pandas 数据框中的列,该列的值中有特殊字符,也没有作为值? - How to convert a column in pandas data frame which has special characters in its values and also None as a value? 将一组数据框行的列值转换为列中的列表 - Convert column values for a group of data frame rows into a list in the column 基于现有数字列、字符串列表作为列名和元组列表作为值在数据框中创建新列 - Create new columns in a data frame based on an existing numeric column, a list of strings as column names and a list of tuples as values 如何从熊猫的数据框中创建列名,行名和值的列表? - How to create a list of column names, row names, and values from data frame in panda? 连接数据框中的值和列名以创建新的数据框 - Concatenate values and column names in a data frame to create a new data frame 如何将 map 字典值转换为具有列表值的数据框列 - How to map dictionary values to data frame column which has values as lists 按行连接两个数据帧,它们在行中具有不同的列名和不同的值 - Concat two data frame row wise which have different column names and different values in row 将具有列表值的字典转换为数据框 - Convert Dictionary of Dictionaries with list values to a data frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM