[英]Add a list in to a pandas data frame column
Hello guys I have this code:大家好,我有这个代码:
check_df = pd.DataFrame()
check_df['item'] = ''
check_df['simillar_items'] = ''
for item in sorted(df_clean.ProductName.unique()):
population = sorted(df_clean.ProductName.unique())
check_df['item'] = [item]
simillar = calculate_similarity(item,population,sensitivity = 85)
check_df['simillar_items'] = simillar
I want to get a dataframe that looks like this:我想得到一个如下所示的数据框:
item simillar_items
item a [Item C, Item B, Item G]
When I run the loop as shown above I get the following error:当我运行如上所示的循环时,出现以下错误:
Length of values (2) does not match length of index (1)
Any idea why?知道为什么吗?
You can create a dictionary, which represents your data, then convert it into a dataframe.您可以创建一个字典来表示您的数据,然后将其转换为数据框。
import pandas as pd
# Create an empty dictionary
data = {
'item': [],
'similar_items': []
}
# Add data to the dictionary
for item in sorted(df_clean.ProductName.unique()):
population = sorted(df_clean.ProductName.unique())
data['item'].append(item)
simillar = calculate_similarity(item,population,sensitivity = 85)
data['similar_items'].append(similar)
# Convert the dictionary into a DataFrame
df = pd.DataFrame(data)
Then, you can find your data in the df
dataframe.然后,您可以在df
数据框中找到您的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.