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

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

Extracting first 3 elements from list of strings in pandas df

问题

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

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

                                                                                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


Please let me know if you need any further assistance.

<details>
<summary>英文:</summary>

I want to extract the first 3 elements from a list of strings from the ```&#39;1/1&#39;``` column.
My ```df_unique``` looks like that:

                                                                                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

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])

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]


And the second solution:

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


gives:

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


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

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


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',]


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']

? I know it can be easy modification but I&#39;ve stuck and messed with that...
Thanks a lot!

</details>


# 答案1
**得分**: 4

以下是您要翻译的内容:

首先将您的字符串转换为读取列表,然后使用 [`str`](https://pandas.pydata.org/docs/reference/api/pandas.Series.str.html) 切片:

```python
import ast
df_unique['1/1'] = df_unique['1/1'].apply(ast.literal_eval)
df_unique['0/0'] = df_unique['0/0'].apply(ast.literal_eval)

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

或者一次性完成:

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

输出:

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]
Name: 1/1, dtype: object
英文:

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

import ast
df_unique[&#39;1/1&#39;] = df_unique[&#39;1/1&#39;].apply(ast.literal_eval)
df_unique[&#39;0/0&#39;] = df_unique[&#39;0/0&#39;].apply(ast.literal_eval)

df_extract_3 = df_unique[&#39;1/1&#39;].str[:3]
print(df_extract_3)

Or in one shot:

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

Output:

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]
Name: 1/1, dtype: object

答案2

得分: 1

尝试这个:

data={"1/1":[['P1-12', 'P1-22', 'P1-25', 'P1-26', 'P1-28', 'P1-6', 'P1-88', 'P1-93'],
        ['P1-12', 'P1-22', 'P1-25', 'P1-26', 'P1-6', 'P1-89', 'P1-92', 'P1-95']],
"0/0":[1,2]}
df=pd.DataFrame(data)

df["1/1"]=df["1/1"].apply(lambda x:x[0:3])
print(df)
                 1/1  0/0
0  [P1-12, P1-22, P1-25]    1
1  [P1-12, P1-22, P1-25]    2
英文:

try this:

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;],
        [&#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;]],
&quot;0/0&quot;:[1,2]}
df=pd.DataFrame(data)

df[&quot;1/1&quot;]=df[&quot;1/1&quot;].apply(lambda x:x[0:3])
print(df)
                     1/1	0/0
0	[P1-12, P1-22, P1-25]	1
1	[P1-12, P1-22, P1-25]	2

答案3

得分: 1

这段代码的翻译如下:

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

Does this work?

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:

确定