简体   繁体   English

在熊猫中附加一个带有列表的数据框作为行

[英]Appending a data frame with list as row in pandas

I am calculating the Levenshtein distance between the input text and a list.我正在计算输入文本和列表之间的 Levenshtein 距离。 I want to store this information in a data frame for further analysis later.我想将此信息存储在数据框中以供以后进一步分析。

I am having issues assigning this list produced by my function to the created dataframe.我在将我的函数生成的这个列表分配给创建的数据框时遇到问题。

col_list = data_df.clean_text_items.unique()
levenshtein_ratio_matrix_df = pd.DataFrame(columns= col_list)

def levenshtein_compare_to_list(row):
    row_levenshtein_ratio = []

    for i in collist:
        row_levenshtein_ratio.append(levenshtein (row['clean_text_source_item'], i))

    levenshtein_ratio_matrix_df.append(row_levenshtein_ratio)

for index, row in data_df.iterrows():
    levenshtein_compare_to_list(row) 

The following runs, and if I print out row_levensthein_ratio per loop it shows the correct values, but it doesn't actually append the data frame.下面运行,如果我打印出每个循环的 row_levensthein_ratio 它显示正确的值,但它实际上并没有附加数据框。 (levenshtein_ratio_matrix_df is left blank) (levenshtein_ratio_matrix_df 留空)

Found the answer找到答案

  df_length = len(levenshtein_ratio_matrix_df)
  levenshtein_ratio_matrix_df.loc[df_length] = row_levenshtein_ratio

Instead of代替

 levenshtein_ratio_matrix_df.append(row_levenshtein_ratio)

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

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