我想要在Python中执行一个特定的操作.实际上,我有一个CSV文件,其中许多数据被指定到列中.我想要的是把它们换成行.然而,我需要尊重具体的规则.

以下是数据集格式:

[...,‘面板编号’,‘性’,‘First_Last_Cand1’,‘VOICE’,‘%VOICE/INS’,‘%VOICE/EXP’,
‘COL_28’,‘COL_29’,‘First_Last_Cand2’,‘COL_32’,‘COL_33’,‘COL_34’,
‘COL_35’,‘COL_36’,‘FIRST_LAST_CAD3’,‘COL_39’,‘COL_40’,‘COL_41’,
‘COL_42’,‘COL_43’,‘First Name_Last_Cand4’,‘COL_46’,‘COL_47’,‘COL_48’,
‘COL_49’,‘COL_50’,‘First Name_Last_Cand5’,‘COL_53’,‘COL_54’,‘COL_55’,
‘COL_56’,‘COL_57’,‘First NAME_LAST_CAD6’,‘COL_60’,‘COL_61’,‘COL_62’, ‘COL_63’,‘COL_64’,‘First NAME_LAST_CAD7’,‘COL_67’,‘COL_68’,‘COL_69’, ‘COL_70’,‘COL_71’,‘First Name_Last_Cand8’,‘COL_74’,‘COL_75’,‘COL_76’, ‘COL_77’,‘COL_78’,‘First NAME_LAST_CAD9’,‘COL_81’,‘COL_82’,‘COL_83’, ‘COL_84’,‘COL_85’,‘First NAME_LAST_AND10’,‘COL_88’,‘COL_89’,‘COL_90’, ‘COL_91’,‘COL_92’,‘First NAME_LAST_CANT11’,‘COL_95’,‘COL_96’,‘COL_97’,
‘COL_98’,‘COL_99’,‘First NAME_LAST_CAND12’,‘COL_102’,‘COL_103’,‘COL_104’]

其中'N°Panneau', 'Sexe', 'Prénom_Nom_Cand1', 'Voix', '% Voix/Ins', '% Voix/Exp'是与'col_{number}', 'col_{number}', 'Prénom_Nom_Cand{number}', 'col_{number}', 'col_{number}', 'col_{number}'相同的列

我只想拥有列'N°Panneau', 'Sexe', 'Prénom_Nom_Cand1', 'Voix', '% Voix/Ins', '% Voix/Exp' containing all the col_...个值等.

如何才能像上面指定的那样(以行的形式获取列)呢?

推荐答案

假设df是您的输入wide-format DataFrame,您可以使用:

out = (
    pd.DataFrame(df.to_numpy().reshape(-1, 6), columns= df.columns[:6])
)

发帖主题:Re:Kolibrios

print(out)

    N°Panneau  Sexe  Prénom_Nom_Cand1  Voix  % Voix/Ins  % Voix/Exp
0           5     8                 9     5           0           0
1           1     7                 6     9           2           4
2           5     2                 4     2           4           7
..        ...   ...               ...   ...         ...         ...
57          9     7                 0     5           2           2
58          8     5                 0     5           9           8
59          6     6                 0     4           7           3

[60 rows x 6 columns]

Input used :

cols = [
    'N°Panneau', 'Sexe', 'Prénom_Nom_Cand1', 'Voix', '% Voix/Ins', '% Voix/Exp',
    'col_28', 'col_29', 'Prénom_Nom_Cand2', 'col_32', 'col_33', 'col_34',
    'col_35', 'col_36', 'Prénom_Nom_Cand3', 'col_39', 'col_40', 'col_41',
    ...
]

np.random.seed(1)

df = pd.DataFrame(np.random.randint(0, 10, (5, len(cols))), columns=cols)


   N°Panneau  Sexe  Prénom_Nom_Cand1  ...  col_102  col_103  col_104
0          5     8                 9  ...        6        8        0
1          2     7                 7  ...        5        4        0
2          7     8                 9  ...        3        8        3
3          5     6                 7  ...        9        1        2
4          0     4                 7  ...        4        7        3

[5 rows x 72 columns]

100

gh_url = "https://raw.githubusercontent.com/kivircik55/dataset/main/" \
         "resultats_par_niveau_burvot_t1_france_entiere_prepared_joined" \
         "_prepared%20(1).csv"

df = pd.read_csv(gh_url)

idx = df.columns.get_loc("Prénom_Nom_Cand1")-2

wide_blocks = df.iloc[:, idx:]

long_blocks = pd.DataFrame(
    wide_blocks.to_numpy().reshape(-1, 6),
    columns= wide_blocks.columns[:6]
)

out = (
    df.iloc[:, :idx].join(
        long_blocks.set_index(
            np.arange(len(df)).repeat(len(long_blocks.columns)*2)))
)

发帖主题:Re:Kolibrios

print(out)

    Année  Code du département Libellé du département  ...  Voix % Voix/Ins  % Voix/Exp
0    2022                   24               Dordogne  ...     2       0.23        0.39
0    2022                   24               Dordogne  ...    13       1.51        2.51
0    2022                   24               Dordogne  ...   139      16.13       26.89
..    ...                  ...                    ...  ...   ...        ...         ...
43   2022                   24               Dordogne  ...    39       4.23        5.86
43   2022                   24               Dordogne  ...     4       0.43         0.6
43   2022                   24               Dordogne  ...    16       1.74         2.4

[528 rows x 29 columns]

Python相关问答推荐

Django关于UniqueBindition的更新

如何在Python中按组应用简单的线性回归?

pyautogui.locateOnScreen在Linux上的工作方式有所不同

symy.分段使用numpy数组

如何使用stride_tricks.as_strided逆转NumPy数组

有什么方法可以避免使用许多if陈述

从包含数字和单词的文件中读取和获取数据集

如何根据条件在多指标框架上进行groupby

LAB中的增强数组

如何计算列表列行之间的公共元素

如何调整spaCy token 化器,以便在德国模型中将数字拆分为行末端的点

max_of_three使用First_select、second_select、

难以在Manim中正确定位对象

有症状地 destruct 了Python中的regex?

如何在Python脚本中附加一个Google tab(已经打开)

pyscript中的压痕问题

使用密钥字典重新配置嵌套字典密钥名

迭代嵌套字典的值

如何使regex代码只适用于空的目标单元格

Polars将相同的自定义函数应用于组中的多个列,