英文:
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 ```'1/1'``` 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'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 'normal' 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'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['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)
Or in one shot:
df_extract_3 = df_unique['1/1'].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={"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
答案3
得分: 1
这段代码的翻译如下:
df_unique['1/1'] = [element[:3] for element in df_unique['1/1']]
英文:
Does this work?
df_unique['1/1'] = [element[:3] for element in df_unique['1/1']]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论