简体   繁体   English

熊猫数据框返回列标题链接到每一行的数据值

[英]Pandas dataframe return column header linked to data value for each row

I am using pandas to read any CSV files and then insert the rows into a database. 我正在使用熊猫读取所有CSV文件,然后将行插入数据库中。 I will be doing this with SQLAlchemy. 我将使用SQLAlchemy进行此操作。

I will not know the header names or size, so it has to be dynamic. 我将不知道标题名称或大小,因此它必须是动态的。 Assume the database rules will ensure data validity. 假设数据库规则将确保数据有效性。

I am trying to map the column header to each data value. 我正在尝试将列标题映射到每个数据值。 See below my current dataframe: 参见下面我当前的数据框:

  Example 1 Example 2 Example 3 Example 4
        Cat       Dog     Mouse     Horse
        Cow       Ant       Pig  Elephant

Here is my desired outputted list: 这是我想要的输出列表:

Example 1=Cat, Example 2=Dog, Example 3=Mouse, Example 4=Horse
Example 1=Cow, Example 2=Ant, Example 3=Pig, Example 4=Elephant

I have tried using zip and iterrows with the below code: 我尝试使用zip和下面的代码进行iterrows

    for index, data in df.iterrows():
        mylist.append(data.values)

    myzip = zip(columns, mylist)

    for z in myzip:
        print(z)

but this is producing one column header per multiple values as seen below: 但这会为多个值产生一个列标题,如下所示:

('Example 1', array(['Cat', 'Dog', 'Mouse', 'Horse'], dtype=object))
('Example 2', array(['Cow', 'Ant', 'Pig', 'Elephant'], dtype=object))

Any help would be greatly appreciated as not sure what function I need to use. 任何帮助将不胜感激,因为不确定我需要使用什么功能。 I'm aware of to_sql but I need to create an insert statement per row. 我知道to_sql但是我需要为每行创建一个插入语句。 Thanks 谢谢

@giser_yugang hits the ideal solution. @giser_yugang可以找到理想的解决方案。 Pandas has inbuilt method DataFrame.to_dict(orient='dict') which converts the dataframe and returns a dictionary, where the key-value pair can be customized using the parameter orient . Pandas具有内置的DataFrame.to_dict(orient='dict') ,该方法可以转换数据帧并返回字典,在其中可以使用参数orient自定义键值对。
'records' amongst the 'orient' gives the kind of result you want. “东方”之中的“记录”给出了您想要的结果。

So your dataframe: 因此,您的数据框:

数据帧

After using: 使用后:

df.to_dict(orient='records')

gives: 得到:

[{'Example 1': 'Cat',
  'Example 2': 'Dog',
  'Example 3': 'Mouse',
  'Example 4': 'Horse'},
 {'Example 1': 'Cow',
  'Example 2': 'Ant',
  'Example 3': 'Pig',
  'Example 4': 'Elephant'}]

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

相关问题 在Pandas数据帧中查找任何单元格值= = x,并返回单元格值,列标题,行和相邻单元格值 - Find any cell values >= x in Pandas dataframe and return cell value, column header, row and neighbouring cell value 如何打印pandas dataframe每一行的索引值、列名和列数据? - How to print index value, column name, and column data for each row of pandas dataframe? Pandas 添加列 header 到行值并转换 dataframe - Pandas add column header to row value and transform dataframe 如何在数据框中拆分一列并将每个值存储为新行(以熊猫为单位)? - How to split a column in a dataframe and store each value as a new row (in pandas)? 将 pandas DataFrame 转换为带有每个值的行和列名称的字典 - Convert a pandas DataFrame to a dict with row and column name for each value 是否有 pandas 方法通过特定列值为每一行添加 dataframe - Is there a pandas way to add a dataframe for each row by a specific column value 在一个 Pandas DataFrame 中找到每一行第二大值的列名 - Find the column name of the second largest value of each row in a Pandas DataFrame Pandas数据帧:返回最大值的行和列 - Pandas dataframe: return row AND column of maximum value(s) 熊猫-列标题到行值 - Pandas - Column Header to Row value 返回 DataFrame [Pandas] 中数据组的 position(行和列) - Return the position (row and column) of a data group in a DataFrame [Pandas]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM