列表列名称ITERTOOLS

发布于 2025-02-12 00:27:16 字数 798 浏览 4 评论 0原文

我有此数据框:

    STATE CITY      TAX_C   MATERIAL    IG  LIMIT
0   TX    DALLAS    1       CARP        0   5
1   TX    DALLAS    1       BLAY        0   10

我已经使用itertools创建了一个循环,该iTertools将每一行的每一列的组合组合在一起:

res = []
for r in range(2,len(df.columns)+1):
    for cols in itertools.combinations(df.columns, r ):
        res += df[list(cols)].T.to_dict('list').values()
res

它给了我这个输出:

[[TX, DALLAS], [TX, DALLAS], [DALLAS, 1], [DALLAS 1], [1, CARP], [1, BLAY], [CARP, 0], [0,5], [TX, 1],...]

我正在尝试获取一个输出,该输出在每个值之前都打印出列名称。 :

[[STATE: 'TX', CITY: 'DALLAS'], [STATE: 'TX', CITY: 'DALLAS'], [CITY: 'DALLAS', TAX_C: '1'], [CITY: 'DALLAS', TAX_C: '1'], [TAX_C: '1', MATERIAL: 'CARP']...]

I have this Dataframe:

    STATE CITY      TAX_C   MATERIAL    IG  LIMIT
0   TX    DALLAS    1       CARP        0   5
1   TX    DALLAS    1       BLAY        0   10

And I've created a loop using itertools that takes the combinations of every column from each row:

res = []
for r in range(2,len(df.columns)+1):
    for cols in itertools.combinations(df.columns, r ):
        res += df[list(cols)].T.to_dict('list').values()
res

And it gives me this output:

[[TX, DALLAS], [TX, DALLAS], [DALLAS, 1], [DALLAS 1], [1, CARP], [1, BLAY], [CARP, 0], [0,5], [TX, 1],...]

I am trying to get an output that prints out the Column name before each value like so:

[[STATE: 'TX', CITY: 'DALLAS'], [STATE: 'TX', CITY: 'DALLAS'], [CITY: 'DALLAS', TAX_C: '1'], [CITY: 'DALLAS', TAX_C: '1'], [TAX_C: '1', MATERIAL: 'CARP']...]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寻梦旅人 2025-02-19 00:27:16

因此,我像这样重现了您的数据

data = [["TX", "TX"], ["DALLAS", "DALLAS"], [1, 1], ["CARP", "BLAY"], [0,0], [5,15]]
df = pd.DataFrame(data).T
df.columns=["STATE", "CITY", "TAX_C", "MATERIAL", "IG", "LIMIT"]

,我认为第一步是更深入地了解如何从数据框架中获取字典,

for key, value in df.to_dict(orient="index").items():
    print(value)

如果

{'STATE': 'TX', 'CITY': 'DALLAS', 'TAX_C': 1, 'MATERIAL': 'CARP', 'IG': 0, 'LIMIT': 5}
{'STATE': 'TX', 'CITY': 'DALLAS', 'TAX_C': 1, 'MATERIAL': 'BLAY', 'IG': 0, 'LIMIT': 15}

我们更深入地,您可以循环浏览它,并附加列表,这样

results = []
for key, value in df.to_dict(orient="index").items():
    row = list(value.items())
    for nr in range((len(value)-1)):
        results.append([list(row[nr]), list(row[nr+1])])

收益

[[['STATE', 'TX'], ['CITY', 'DALLAS']],
 [['CITY', 'DALLAS'], ['TAX_C', 1]],
 [['TAX_C', 1], ['MATERIAL', 'CARP']],
 [['MATERIAL', 'CARP'], ['IG', 0]],
 [['IG', 0], ['LIMIT', 5]],
 [['STATE', 'TX'], ['CITY', 'DALLAS']],
 [['CITY', 'DALLAS'], ['TAX_C', 1]],
 [['TAX_C', 1], ['MATERIAL', 'BLAY']],
 [['MATERIAL', 'BLAY'], ['IG', 0]],
 [['IG', 0], ['LIMIT', 15]]]

的 请注意,您在Python中无法描述。某物是列表或词典。列表仅与逗号相隔。

我希望这会有所帮助:)

So I reproduced your data like so

data = [["TX", "TX"], ["DALLAS", "DALLAS"], [1, 1], ["CARP", "BLAY"], [0,0], [5,15]]
df = pd.DataFrame(data).T
df.columns=["STATE", "CITY", "TAX_C", "MATERIAL", "IG", "LIMIT"]

And I think the first step is to go a little deeper into how you can get dictionary out of the dataframe

for key, value in df.to_dict(orient="index").items():
    print(value)

Which yields

{'STATE': 'TX', 'CITY': 'DALLAS', 'TAX_C': 1, 'MATERIAL': 'CARP', 'IG': 0, 'LIMIT': 5}
{'STATE': 'TX', 'CITY': 'DALLAS', 'TAX_C': 1, 'MATERIAL': 'BLAY', 'IG': 0, 'LIMIT': 15}

If we go a little deeper you can loop over it and append a list like so

results = []
for key, value in df.to_dict(orient="index").items():
    row = list(value.items())
    for nr in range((len(value)-1)):
        results.append([list(row[nr]), list(row[nr+1])])

yielding

[[['STATE', 'TX'], ['CITY', 'DALLAS']],
 [['CITY', 'DALLAS'], ['TAX_C', 1]],
 [['TAX_C', 1], ['MATERIAL', 'CARP']],
 [['MATERIAL', 'CARP'], ['IG', 0]],
 [['IG', 0], ['LIMIT', 5]],
 [['STATE', 'TX'], ['CITY', 'DALLAS']],
 [['CITY', 'DALLAS'], ['TAX_C', 1]],
 [['TAX_C', 1], ['MATERIAL', 'BLAY']],
 [['MATERIAL', 'BLAY'], ['IG', 0]],
 [['IG', 0], ['LIMIT', 15]]]

Please note that you description is not possible in Python. Something is a list or a dictionary. A list is separated with comma's only.

I hope this helps :)

一直在等你来 2025-02-19 00:27:16

尝试以下代码:

res = []
for r in range(2, df.columns.size + 1):
    for cols in itertools.combinations(df.columns, r):
        res += df[list(cols)].T.to_dict().values()
res

区别在于我将参数从 to_dict 中删除,因此它可以与 dict 的默认方向一起工作。

结果的最初部分是:

[{'STATE': 'TX', 'CITY': 'DALLAS'},
 {'STATE': 'TX', 'CITY': 'DALLAS'},
 {'STATE': 'TX', 'TAX_C': 1},
 {'STATE': 'TX', 'TAX_C': 1},
 {'STATE': 'TX', 'MATERIAL': 'CARP'},
 {'STATE': 'TX', 'MATERIAL': 'BLAY'},
 {'STATE': 'TX', 'IG': 0},
 {'STATE': 'TX', 'IG': 0},
 {'STATE': 'TX', 'LIMIT': 5},
 {'STATE': 'TX', 'LIMIT': 10},

因此,它是词典的列表,与您所需的结果非常相似。

Try the following code:

res = []
for r in range(2, df.columns.size + 1):
    for cols in itertools.combinations(df.columns, r):
        res += df[list(cols)].T.to_dict().values()
res

The difference is that I dropped the argument from to_dict, so it works with the default orientation of dict.

The initial part of the result is:

[{'STATE': 'TX', 'CITY': 'DALLAS'},
 {'STATE': 'TX', 'CITY': 'DALLAS'},
 {'STATE': 'TX', 'TAX_C': 1},
 {'STATE': 'TX', 'TAX_C': 1},
 {'STATE': 'TX', 'MATERIAL': 'CARP'},
 {'STATE': 'TX', 'MATERIAL': 'BLAY'},
 {'STATE': 'TX', 'IG': 0},
 {'STATE': 'TX', 'IG': 0},
 {'STATE': 'TX', 'LIMIT': 5},
 {'STATE': 'TX', 'LIMIT': 10},

so it is a list of dictionaries, quite similar to your desired result.

流绪微梦 2025-02-19 00:27:16

我认为您正在寻找pairwise而不是组合 来自itertools -

pairs = []
for col_pair in pairwise(df.columns):
    values = df[list(col_pair)].to_records(index=False)
    keys = col_pair
    pairs.extend(dict(zip(col_pair, _)) for _ in values)

output

[{'STATE': 'TX', 'CITY': 'DALLAS'},
 {'STATE': 'TX', 'CITY': 'DALLAS'},
 {'CITY': 'DALLAS', 'TAX_C': 1},
 {'CITY': 'DALLAS', 'TAX_C': 1},
 {'TAX_C': 1, 'MATERIAL': 'CARP'},
 {'TAX_C': 1, 'MATERIAL': 'BLAY'},
 {'MATERIAL': 'CARP', 'IG': 0},
 {'MATERIAL': 'BLAY', 'IG': 0},
 {'IG': 0, 'LIMIT': 5},
 {'IG': 0, 'LIMIT': 10}]

I think you are looking for pairwise and not combinations from itertools -

pairs = []
for col_pair in pairwise(df.columns):
    values = df[list(col_pair)].to_records(index=False)
    keys = col_pair
    pairs.extend(dict(zip(col_pair, _)) for _ in values)

Output

[{'STATE': 'TX', 'CITY': 'DALLAS'},
 {'STATE': 'TX', 'CITY': 'DALLAS'},
 {'CITY': 'DALLAS', 'TAX_C': 1},
 {'CITY': 'DALLAS', 'TAX_C': 1},
 {'TAX_C': 1, 'MATERIAL': 'CARP'},
 {'TAX_C': 1, 'MATERIAL': 'BLAY'},
 {'MATERIAL': 'CARP', 'IG': 0},
 {'MATERIAL': 'BLAY', 'IG': 0},
 {'IG': 0, 'LIMIT': 5},
 {'IG': 0, 'LIMIT': 10}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文