簡體   English   中英

從Python中的數據框中提取String

[英]Extracting String from dataframe in Python

我有一個包含“genres列”的數據框,它的結構如下:

0    [{"id": 28, "name": "Action"}, {"id": 12, "nam..."
1    [{"id": 12, "name": "Adventure"}, {"id": 14, "..."
2    [{"id": 28, "name": "Action"}, {"id": 12, "nam..."
3    [{"id": 28, "name": "Action"}, {"id": 80, "nam..."
4    [{"id": 28, "name": "Action"}, {"id": 12, "nam..."
Name: genres, dtype: object

我只是想提取像“動作”“冒險”等類型。

我使用了不同的方法,但都沒有實現。

d2 = pd.read_csv(r"C:\Users\nxx\PycharmProjects\tmdb_5000_movies.csv")
d2=pd.DataFrame(d2)
d2["genres"] = re.search(" (.?)}",d2["genres"]).group(1,2)

執行上面的代碼時,我得到以下錯誤。

Name: genres, dtype: object
    return _compile(pattern, flags).search(string)
TypeError: expected string or bytes-like object

我試圖將列轉換為str,但它不起作用。

我也試過了。

x=["Action","Crime","Horror","Drama","Comedy","Romance","Thriller","Documentary","Family","Adventure","Animation" "Science Fiction","Mystery","Fantasy","War","History","Music"]


d2["genres"] = d2["genres"].apply(lambda x: ", ".join(x["Action"] for i in x)) print(d2["genres"]) 
d2["genres"] = d2["genres"].apply(lambda x: ", ".join(x["Action"] for i in x)) TypeError: string indices must be integers –

如果需要從列表中提取名稱

采用:

d2 = pd.read_csv(r"C:\Users\nxx\PycharmProjects\tmdb_5000_movies.csv")
d2["genres"] = d2["genres"].apply(lambda x: ", ".join(i["name"] for i in x))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM