簡體   English   中英

如何將 select 的第一個元素存儲在元組列表中?

[英]How to select the first elements stored in a list of tuples?

我需要提取/清除存儲在元組列表中的值,只保留括號中每個元素的第一個(左)記錄。

我將放置當前表格以及我如何需要這些信息,以便更容易看到問題。

當前df:

ID 和弦
1 [(N, 0.371519274), (A7, 0.464399092)]
2 [(N, 0.371519274), (Em, 0.464399092)]

所需的df:

ID 和弦
1 N, A7
2 N, 埃姆

這可能是所需的 df 嗎?

假設chords列的值是元組列表

df['chords'] = df['chords'].map(lambda lst: ", ".join(tup[0] for tup in lst))
df['chords'] = df['chords'].astype('str').str.split(r'[\[\](), ]+').apply(lambda x: (x[1], x[3]))

Output:

>>> df
   ID   chords
0   1  (N, A7)
1   2  (N, Em)
df['chords'] = df['chords'].str.extract('\(([^,]+).+\(([^,]+)').apply(tuple, axis=1)
print(df)
   ID   chords
0   1  (N, A7)
1   2  (N, Em)

暫無
暫無

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

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