自动转换为新数据

2024-10-01 19:33:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python的新手,我在StackOverflow中搜索我的案例,但是我找不到技术上的答案。我有很多英国人。你知道吗

我的问题是这样的,我有一个dataframe

df
BS          N
BS1 - BS5   1
BS2 - BS7   2
BS1 - BS9   2
BS9 - BS1   1

我想自动生成新数据。我的预期结果如下:

New_BS  BS1 - BS5   BS2 - BS7   BS1 - BS9   BS9 - BS1   Total
BS1-2       1                       2                     3
BS2-3       1           2           2                     5
BS3-4       1           2           2                     5
BS4-5       1           2           2                     5
BS5-6                   2           2                     4
BS6-7                   2           2                     4
BS7-8                               2                     2
BS8-9                               2                     2
BS9-8                                            1        1
BS8-7                                            1        1
BS7-6                                            1        1
BS6-5                                            1        1
BS5-4                                            1        1
BS4-3                                            1        1
BS3-2                                            1        1
BS2-1                                            1        1

事先谢谢你帮助我


Tags: 答案bsstackoverflow技术案例新手bs4bs3
1条回答
网友
1楼 · 发布于 2024-10-01 19:33:02

嗯-这是一个完全黑客-但它很有趣。。。你知道吗

import pandas as pd
import numpy as np

df = df_flat = pd.DataFrame({"BS": ['BS1 - BS5', 'BS2 - BS7', 'BS1 - BS9', 'BS9 - BS1'],
                   "N" : [1, 2, 2, 1]})

df = df.pivot(columns='BS',
              values='N')

df_flat = df_flat.pivot_table(
              columns='BS',
              values='N')

for column_name, column in zip(list(df), df):
    if int(column[2:3]) < int(column[8:9]):
        for stop in range(int(column[2:3]), int(column[8:9])):
            index = "BS" + str(stop) + "-" + str(stop + 1)
            if index not in list(df.index.values):
                df.loc[index] = np.nan
            df.loc[index, column] = df_flat.loc['N', column]
    else:
        for stop in range(int(column[2:3]), int(column[8:9]), -1):
            index = "BS" + str(stop) + "-" + str(stop - 1)
            if index not in list(df.index.values):
                df.loc[index] = np.nan
            df.loc[index, column] = df_flat.loc['N', column]

df['Total'] = df.sum(axis=1)

df = df.iloc[len(list(df_flat)):]

print(df.fillna(''))

输出

$ python bus.py
BS    BS1 - BS5 BS1 - BS9 BS2 - BS7 BS9 - BS1  Total
BS1-2         1         2                        3.0
BS2-3         1         2         2              5.0
BS3-4         1         2         2              5.0
BS4-5         1         2         2              5.0
BS5-6                   2         2              4.0
BS6-7                   2         2              4.0
BS7-8                   2                        2.0
BS8-9                   2                        2.0
BS9-8                                       1    1.0
BS8-7                                       1    1.0
BS7-6                                       1    1.0
BS6-5                                       1    1.0
BS5-4                                       1    1.0
BS4-3                                       1    1.0
BS3-2                                       1    1.0
BS2-1                                       1    1.0

有大约1000种方法可以改善这一点-但这是一个好的开始。。。你知道吗

请注意,切片是数据集的一个非常重要的约束条件--您必须真正地修改它以使其成为动态的。你知道吗

相关问题 更多 >

    热门问题