简体   繁体   English

如何使用 Python 中另一个列表的元素类型创建列表

[英]How to create a list with the types of element of another list in Python

I want to create a DataFrame with one instance per columns of my data and the type of that instance but I can't find a way to create a list of the types (in order to put it in the DataFrame).我想创建一个 DataFrame,其中每列数据有一个实例以及该实例的类型,但我找不到创建类型列表的方法(以便将其放入 DataFrame)。

Here is an example of what I want:这是我想要的一个例子:

my_original_data = pd.DataFrame({'col1': [1,2,3],'col2':['Hello','foo','bar'],'col3':[dt.datetime(2000,1,1),dt.datetime(1999,12,2),dt.datetime(1950,5,3)]})

我的原始数据

And I want a new DataFrame with first row = columns names, second row = first values, ie [1, 'Hello', dt.datetime(2000,1,1)] and third row = types of these values, ie [int, str, date].我想要一个新的 DataFrame,第一行 = 列名,第二行 = 第一个值,即 [1, 'Hello', dt.datetime(2000,1,1)] 和第三行 = 这些值的类型,即 [int , str, 日期]。

How to create this last line?如何创建最后一行?

Before to transform three lists into DataFrame, I tried在将三个列表转换为 DataFrame 之前,我尝试过

first_values = [my_original_data.loc[0,column] for column in df.columns]
types = [type(my_original_data.loc[0,column]) for column in df.columns]

It returns "TypeError: 'list' object is not callable".它返回“TypeError: 'list' object is not callable”。 Same if I try如果我尝试也一样

types = map(type,first_values)
list(types)

Try this:尝试这个:

cols = my_original_data.columns.tolist()
data = my_original_data.loc[0].tolist()
dtypes = list(map(type, data))

new_df = pd.DataFrame([[cols], [data], [dtypes]], columns=['df_desc'])

Similar to what u tried:类似于你尝试过的:

first = my_original_data.iloc[0,:].values
types = [type(my_original_data.iloc[0,column]) for column in range(len(my_original_data.columns))]

final_df = pd.DataFrame([first,types],index=['first','type'],columns = my_original_data.columns)

暂无
暂无

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

相关问题 Python 如果元素存在于另一个列表中,则创建 1 个元素的列表 - Python create list of 1 element if the element exist in another list 如何创建一个新列表,每个元素都是python中另一个列表的每个元素的正则表达式? - How can I create a new list with each element a regular expression of each element of another list in python? 如何创建另一个列表python的重复列表 - How to create list of duplicates of another list python 如何拆分列表中的某些元素以在 python 中创建另一个列表? - How do I split certain element within a list to create another list in python? 如何获得列表中元素的总出现次数以及如何使用此信息创建另一个列表: - How to get total occurrence of element in a list and create another list with this information: 如何创建一个包含元素 0 n 次的列表? python - How to create a list with the element 0 n times? python 如何将Python列表中的元素移动到列表中的其他位置(而不是末尾) - How to move an element in a Python list to another position in the list (not the end) Python-如何按另一个列表列表的最后一个元素对列表列表进行排序? - Python - How to sort a list of lists by last element of another list of lists? 如何使列表中的每个元素在python中成为另一个列表 - How to make each element in a list another list in python 如何将列表元素作为索引号在 Python 中打印另一个列表? - How to take a list element as an index number to print another list in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM