数据框中的pandas迭代列和行,以将组合的输出扫描到列表

问题描述 投票:0回答:1

我想打印带有列名的迭代列和行的字符串,也存储在列表 enter image description here

循环打印字符串并保存在列表中: Apple =输出[COL7:COL0]enter image description here bana =输出[COL7:COL4]

猫=输出[col3:col0]

dog=输出[Col3:Col0]

.....

为您的巨大帮助!

您可以

ffill

然后重塑:

out = (df .ffill(axis=1).rename_axis(index='idx', columns='col') .replace('-', pd.NA) .stack() .reset_index(name='value') .groupby(['idx', 'value'], as_index=False)['col'] .agg(lambda x: f'{x.min()}:{x.max()}') .assign(n = lambda x: x.groupby('idx').cumcount(), value=lambda x: x['value']+'='+x.pop('col')) .pivot(index='idx', columns='n', values='value') .rename_axis(index=None, columns=None) )
python pandas dataframe for-loop
1个回答
0
投票
输出:

                  0               1             2             3             4
0   Apple=col0:col7             NaN           NaN           NaN           NaN
1  Banana=col4:col7   Cat=col0:col3           NaN           NaN           NaN
2     Dog=col0:col3             NaN           NaN           NaN           NaN
3     Egg=col1:col4  Fish=col0:col0           NaN           NaN           NaN
4      G1=col4:col4    G2=col3:col3  G3=col2:col2  G4=col1:col1  G5=col0:col0
5      HA=col6:col6    HB=col5:col5  HC=col4:col4  HD=col0:col3           NaN


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.