Python 与文本文件的每个元素关联的属性列表
我有一个文本文件:Python 与文本文件的每个元素关联的属性列表,python,pandas,Python,Pandas,我有一个文本文件: Name Attribute A b1 B b2 C b3 B b4 A b5 C b7 我希望输出是 Name Attribute A (b1,b5) B (b2,b4) C (b3,b7) 对此有什么python/pandas建议吗?试试以下方法: In [301]: df.groupby('Name')['Attribut
Name Attribute
A b1
B b2
C b3
B b4
A b5
C b7
我希望输出是
Name Attribute
A (b1,b5)
B (b2,b4)
C (b3,b7)
对此有什么python/pandas建议吗?试试以下方法:
In [301]: df.groupby('Name')['Attribute'].apply(lambda x: tuple(x.tolist())).reset_index()
Out[301]:
Name Attribute
0 A (b1, b5)
1 B (b2, b4)
2 C (b3, b7)
试试这个,没有lambda:
df.groupby('Name')['Attribute'].apply(tuple).reset_index()
这就是为什么要制作字典:
myfile = open('yourfile.txt','r').readlines()
mydict = {}
for line in myfile[1:]:
data = line.split()
if data[0] in mydict:
mydict[data[0]].append(data[1])
else:
mydict[data[0]] = [data[1]]