简体   繁体   English

列表理解迭代 dataframe

[英]List comprehension to iterate through dataframe

I have written code to encode one row of a dataframe to json, as follows:我已经编写代码将 dataframe 的一行编码为 json,如下所示:

def encode_df_metadata_row(df):
    return {'name': df['Title'].values[0], 'code': df['Code'].values[0], 'frequency': df['Frequency'].values[0], 'description': df['Subtitle'].values[0], 'source': df['Source'].values[0]}

Now I would like to encode an entire dataframe to json with some transformation, so I wrote this function:现在我想通过一些转换将整个 dataframe 编码为 json,所以我写了这个 function:

def encode_metadata_list(df_metadata):
    return [encode_df_metadata_row(df_row) for index, df_row in df_metadata.iterrows()]

I then try to call the function using this code:然后我尝试使用以下代码调用 function:

df_oodler_metadata = pd.read_csv('DATA\oodler-datasets-metadata.csv')
response = encode_metadata_list(df_oodler_metadata)
print(response)

When I run this code, I get the following error:当我运行此代码时,出现以下错误:

AttributeError: 'str' object has no attribute 'values'

I've tried a bunch of variations but I keep getting similar errors.我尝试了很多变体,但我不断收到类似的错误。 Does someone know the right way to do this?有人知道这样做的正确方法吗?

DataFrame.iterrows yields pairs of index and row , where each row is a Series object. It stores a single element for each column, so the .values[0] part in your encode_df_metadata_row(df) function becomes irrelevant - the correct form of this function should be: DataFrame.iterrows产生成对的indexrow ,其中每一row都是一个系列object。它为每一列存储一个元素,因此encode_df_metadata_row(df) function 中的.values[0]部分变得无关紧要 - 这个的正确形式function 应该是:

def encode_df_metadata_row(row):
    return {'name': row['Title'], 'code': row['Code'], 'frequency': row['Frequency'], 'description': row['Subtitle'], 'source': row['Source']}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM