从pandas数据框中提取字符串列表的前3个元素。

huangapple go评论102阅读模式
英文:

Extracting first 3 elements from list of strings in pandas df

问题

Sure, here's the translation of the code portion you provided:

  1. 我想要从```'1/1'```列的字符串列表中提取前3个元素。
  2. 我的```df_unique```如下所示
  1. 1/1 0/0 count

0 ['P1-12', 'P1-22', 'P1-25', 'P1-26', 'P1-28', 'P1-6', 'P1-88', 'P1-93'] ['P1-89', 'P1-90', 'P1-92', 'P1-95'] 1
1 ['P1-12', 'P1-22', 'P1-25', 'P1-26', 'P1-6', 'P1-89', 'P1-92', 'P1-95'] ['P1-28', 'P1-90', 'P1-93'] 1
2 ['P1-12', 'P1-22', 'P1-25', 'P1-26', 'P1-88', 'P1-89', 'P1-92', 'P1-93', 'P1-95'] ['P1-28', 'P1-6', 'P1-90'] 1
3 ['P1-12', 'P1-22', 'P1-25', 'P1-26', 'P1-88', 'P1-89', 'P1-92', 'P1-93'] ['P1-28', 'P1-6', 'P1-90'] 1

  1. Please let me know if you need any further assistance.
  2. <details>
  3. <summary>英文:</summary>
  4. I want to extract the first 3 elements from a list of strings from the ```&#39;1/1&#39;``` column.
  5. My ```df_unique``` looks like that:
  1. 1/1 0/0 count

0 ['P1-12', 'P1-22', 'P1-25', 'P1-26', 'P1-28', 'P1-6', 'P1-88', 'P1-93'] ['P1-89', 'P1-90', 'P1-92', 'P1-95'] 1
1 ['P1-12', 'P1-22', 'P1-25', 'P1-26', 'P1-6', 'P1-89', 'P1-92', 'P1-95'] ['P1-28', 'P1-90', 'P1-93'] 1
2 ['P1-12', 'P1-22', 'P1-25', 'P1-26', 'P1-88', 'P1-89', 'P1-92', 'P1-93', 'P1-95'] ['P1-28', 'P1-6', 'P1-90'] 1
3 ['P1-12', 'P1-22', 'P1-25', 'P1-26', 'P1-88', 'P1-89', 'P1-92', 'P1-93'] ['P1-28', 'P1-6', 'P1-90'] 1

  1. I&#39;ve tried to use different solutions:

df_extract_3 = df_unique['1/1'].str.split().map(lambda lst: [string[0:3] for string in lst])

  1. but the result looks like that:

0 [['P, 'P1, 'P1, 'P1, 'P1, 'P1, 'P1, 'P1]
1 [['P, 'P1, 'P1, 'P1, 'P1, 'P1, 'P1, 'P1]
2 [['P, 'P1, 'P1, 'P1, 'P1, 'P1, 'P1, 'P1, 'P1]
3 [['P, 'P1, 'P1, 'P1, 'P1, 'P1, 'P1, 'P1]

  1. And the second solution:

df_extract_3 = df_unique['1/1'].str[0:3]

  1. gives:

0 ['P
1 ['P
2 ['P
3 ['P

  1. When I try to add ```split``` :

df_extract_3 = df_unique['1/1'].str.split().str[0:3]

  1. the final result is:

0 [['P1-12',, 'P1-22',, 'P1-25',]
1 [['P1-12',, 'P1-22',, 'P1-25',]
2 [['P1-12',, 'P1-22',, 'P1-25',]
3 [['P1-12',, 'P1-22',, 'P1-25',]

  1. What should I change to receive &#39;normal&#39; output like:

0 ['P1-12', 'P1-22', 'P1-25']
1 ['P1-12', 'P1-22', 'P1-25']
2 ['P1-12', 'P1-22', 'P1-25']
3 ['P1-12', 'P1-22', 'P1-25']

  1. ? I know it can be easy modification but I&#39;ve stuck and messed with that...
  2. Thanks a lot!
  3. </details>
  4. # 答案1
  5. **得分**: 4
  6. 以下是您要翻译的内容:
  7. 首先将您的字符串转换为读取列表,然后使用 [`str`](https://pandas.pydata.org/docs/reference/api/pandas.Series.str.html) 切片:
  8. ```python
  9. import ast
  10. df_unique['1/1'] = df_unique['1/1'].apply(ast.literal_eval)
  11. df_unique['0/0'] = df_unique['0/0'].apply(ast.literal_eval)
  12. df_extract_3 = df_unique['1/1'].str[:3]
  13. print(df_extract_3)

或者一次性完成:

  1. df_extract_3 = df_unique['1/1'].apply(lambda x: ast.literal_eval(x)[:3])

输出:

  1. 0 [P1-12, P1-22, P1-25]
  2. 1 [P1-12, P1-22, P1-25]
  3. 2 [P1-12, P1-22, P1-25]
  4. 3 [P1-12, P1-22, P1-25]
  5. Name: 1/1, dtype: object
英文:

First convert your strings to read lists, then slice with str:

  1. import ast
  2. df_unique[&#39;1/1&#39;] = df_unique[&#39;1/1&#39;].apply(ast.literal_eval)
  3. df_unique[&#39;0/0&#39;] = df_unique[&#39;0/0&#39;].apply(ast.literal_eval)
  4. df_extract_3 = df_unique[&#39;1/1&#39;].str[:3]
  5. print(df_extract_3)

Or in one shot:

  1. df_extract_3 = df_unique[&#39;1/1&#39;].apply(lambda x: ast.literal_eval(x)[:3])

Output:

  1. 0 [P1-12, P1-22, P1-25]
  2. 1 [P1-12, P1-22, P1-25]
  3. 2 [P1-12, P1-22, P1-25]
  4. 3 [P1-12, P1-22, P1-25]
  5. Name: 1/1, dtype: object

答案2

得分: 1

尝试这个:

  1. data={"1/1":[['P1-12', 'P1-22', 'P1-25', 'P1-26', 'P1-28', 'P1-6', 'P1-88', 'P1-93'],
  2. ['P1-12', 'P1-22', 'P1-25', 'P1-26', 'P1-6', 'P1-89', 'P1-92', 'P1-95']],
  3. "0/0":[1,2]}
  4. df=pd.DataFrame(data)
  5. df["1/1"]=df["1/1"].apply(lambda x:x[0:3])
  1. print(df)
  2. 1/1 0/0
  3. 0 [P1-12, P1-22, P1-25] 1
  4. 1 [P1-12, P1-22, P1-25] 2
英文:

try this:

  1. data={&quot;1/1&quot;:[[&#39;P1-12&#39;, &#39;P1-22&#39;, &#39;P1-25&#39;, &#39;P1-26&#39;, &#39;P1-28&#39;, &#39;P1-6&#39;, &#39;P1-88&#39;, &#39;P1-93&#39;],
  2. [&#39;P1-12&#39;, &#39;P1-22&#39;, &#39;P1-25&#39;, &#39;P1-26&#39;, &#39;P1-6&#39;, &#39;P1-89&#39;, &#39;P1-92&#39;, &#39;P1-95&#39;]],
  3. &quot;0/0&quot;:[1,2]}
  4. df=pd.DataFrame(data)
  5. df[&quot;1/1&quot;]=df[&quot;1/1&quot;].apply(lambda x:x[0:3])
  1. print(df)
  2. 1/1 0/0
  3. 0 [P1-12, P1-22, P1-25] 1
  4. 1 [P1-12, P1-22, P1-25] 2

答案3

得分: 1

这段代码的翻译如下:

  1. df_unique['1/1'] = [element[:3] for element in df_unique['1/1']]
英文:

Does this work?

  1. df_unique[&#39;1/1&#39;] = [element[:3] for element in df_unique[&#39;1/1&#39;]]

huangapple
  • 本文由 发表于 2023年6月29日 20:16:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定