【问题标题】:How to assign different values from a string to new column? [closed]如何将字符串中的不同值分配给新列? [关闭]
【发布时间】:2022-01-03 05:19:41
【问题描述】:

Dialog_act 是我的标签

我需要分配 int 值,例如 (inform_pricerange=1, inform_area=2, request_food=3, inform_food=4...) 目标是如下所示:

1,2    
1,2,3    
4    
5,2,4    
6

CSV(5 行):

"transcript_id  who transcript  dialog_act    
0   USR  I need to find an expensive restauant that's in the south section of the city.     inform_pricerange; inform_area;    
1   SYS  There are several restaurants in the south part of town that serve expensive food. Do you have a cuisine preference?   inform_pricerange; inform_area; request_food;    
2   USR  No I don't care about the type of cuisine.     inform_food;    
3   SYS  Chiquito Restaurant Bar is a Mexican restaurant located in the south part of town.     inform_name; inform_area; inform_food;    
4   USR  What is their address?     request_address;    
5   SYS  There address is 2G Cambridge Leisure Park Cherry Hinton Road Cherry Hinton, it there anything else I can help you with?   inform_address;"

我该怎么做?

感谢您的建议

【问题讨论】:

  • 你能贴出重现数据框的代码吗,as opposed to an image of text
  • 数据帧来自一个csv文件,就像那样
  • 请将您的图片替换为纯文本数据。
  • 但它只是“df_full = pd.read_csv(csvFile)”
  • 复制/粘贴文件内容(前五行)

标签: python pandas


【解决方案1】:

更新

我想定义值,而不是升序

vals_to_replace = {'inform_pricerange': 1, 'inform_area': 2, 'request_food': 3,
                   'inform_food': 4, 'inform_name': 5, 'request_address': 6,
                   'inform_address': 7}

df['dialog_act'] = df['dialog_act'].str.strip(';').str.split('; ').explode() \
                      .map(vals_to_replace).astype(str) \
                      .groupby(level=0).apply(', '.join)
print(df)

# Output
  dialog_act
0       1, 2
1    1, 2, 3
2          4
3    5, 2, 4
4          6
5          7

旧答案

尝试将explode您的列转换为标量值列表并使用pd.factorize

# Step 1: explode
df1 = df['dialog_act'].str.strip(';').str.split('; ').explode().to_frame()

# Step 2: factorize
df['dialog_act'] = df1.assign(dialog_act=pd.factorize(df1['dialog_act'])[0] + 1) \
                      .astype(str).groupby(level=0)['dialog_act'].apply(', '.join)

输出:

>>> df
  dialog_act
0       1, 2
1    1, 2, 3
2          4
3    5, 2, 4
4          6
5          7

>>> df1
          dialog_act
0  inform_pricerange
0        inform_area
1  inform_pricerange
1        inform_area
1       request_food
2        inform_food
3        inform_name
3        inform_area
3        inform_food
4    request_address
5     inform_address

【讨论】:

  • 不是这样,因为我想定义值,而不是按升序排列
  • 我更新了我的答案。请检查一下好吗?
【解决方案2】:

我认为你可以使用 Pandas dataframe.replace() 方法。首先,将您的表格转换为 Pandas Dataframe。那么,

vals_to_replace = {'inform_pricerange':1, 'inform_area':2, 'request_food':3, 'inform_food': 4}
your_df = your_df.replace({'your_label':vals_to_replace})

我看到一个关于pandas replace multiple values one column 的类似问题。

【讨论】:

  • 谢谢,但这不起作用,因为我每行有超过 1 个标签
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-28
相关资源
最近更新 更多