我有这个数据框:
Col1 Col2 Col3 Col4
1| A | auto | big | red
2| A | moto | big | red
3| B | auto | small | blue
4| C | auto | small | green
5| C | auto | big | red
6| C | moto | small | blue
7| C | moto | small | black
我想以以下形式导出谷歌表单:
Col1 Col2 Col3 Col4
1| A | | |
2| A | auto | |
3| A | auto | big |
4| A | auto | big | red
5| A | moto | |
6| A | moto | big |
7| A | moto | big | red
8| B | | |
9| B | auto | |
10| B | auto | small |
11| B | auto | small | blue
12| C | | |
13| C | auto | |
14| C | auto | small |
15| C | auto | small | green
16| C | auto | big |
17| C | auto | big | red
18| C | moto | |
19| C | moto | small |
20| C | moto | small | blue
21| C | moto | small | black
我曾尝试将数据帧转换成字典,但后来我被困在如何“打印”它的问题上
这是我的密码:
import numpy as np
import pandas as pd
from collections import defaultdict
def get_tree( data ):
df = data.filter( [ 'Col1', 'Col2', 'Col3', 'Col4' ], axis='columns' )
d = defaultdict( lambda: defaultdict(list) )
for row in df.itertuples():
d[ row[1] ][ row[2] ][ row[3] ].append( row[4] )
return d
有什么提示吗
与数据帧
df
类似这个
为您提供以下数据帧(以及相应的字典
data
)这就是你要找的吗
相关问题 更多 >
编程相关推荐