[英]List of lists to a Pandas dataframe
Hi I am trying to make a look up list, that given a listID I can find the users who have it, and given a UserID I can find all lists of that user. 嗨,我正在尝试创建一个查找列表,给定一个listID,我可以找到拥有它的用户,给定一个UserID,我可以找到该用户的所有列表。
The data comes in this format: 数据采用以下格式:
[['34', '345'],
['12', '23,534,34'],
['1', '13,42']]
What I would like is a pandas dataframe that looks like: 我想要的是一个熊猫数据框,看起来像:
UserID, ListID
34, 345
12, 23
12, 534
12, 34
1, 13
1, 42
My thoughts were to make the second string to a list splitting on 'commas', but from there I am stuck. 我的想法是将第二个字符串添加到以“逗号”分隔的列表中,但从那开始我陷入了困境。 Any suggestions? 有什么建议么?
You should clean up your data before feeding it into the data frame constructor. 您应该先清理数据,然后再将其输入数据框架构造函数。 Here is a simple script: 这是一个简单的脚本:
import pandas as pd
data = [['34', '345'],
['12', '23,534,34'],
['1', '13,42']]
new_data = []
for row in data:
x, yvals = row
for y in yvals.split(','):
new_data.append([x,y])
df = pd.DataFrame(new_data, columns=['UserID', 'ListID'])
Here's one way 这是一种方法
In [386]: L = [['34', '345'], ['12', '23,534,34'], ['1', '13,42']]
In [387]: (pd.DataFrame(L, columns=['UserID', 'ListID'])
.set_index('UserID')
.ListID.str.split(',')
.apply(pd.Series)
.stack()
.reset_index(level=0, name='ListID'))
Out[387]:
UserID ListID
0 34 345
1 12 23
2 12 534
3 12 34
4 1 13
5 1 42
You can do as follow : 您可以执行以下操作:
df_tmp = pd.DataFrame([['34', '345'],
['12', '23,534,34'],
['1', '13,42']], columns=['ListID', 'UserIDs'])
s = df_tmp['UserIDs'].str.split(',', expand=True).stack()
i = s.index.get_level_values(0)
df = df_tmp.loc[i].copy()
df["UserID"] = s.values
del df['UserIDs']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.