创建一个从pandas DataFrame生成的数值表格。

huangapple go评论59阅读模式
英文:

Create a table of values from a pandas DataFrame

问题

我正在尝试在Python中根据用户输入创建一个关于除草剂使用的应用程序和日期的表格。我有一个带有应用程序信息('DeviceName')和日期('DAY')的pandas DataFrame。我从DataFrame中获取唯一的日期和设备名称:

devices = df['DeviceName'].unique()
days = df['DAY'].unique()

我从用户那里获取了除草剂的使用量:

herbicides = []

for device in devices:
    for day in days:
        herbicide = input("How much herbicide did " + str(device) + " use on Day " + str(day) + "?: ")
        herbicides.append(herbicide)

如果可能的话,我希望最终的输出显示用户输入,如下所示:

   DeviceName          24       25       26       27       28
Applied_AA-19     Input #  Input #  Input #  Input #  Input #
Applied_AA-24     Input #  Input #  Input #  Input #  Input #
英文:

I'm trying to create a table of herbicide use by applicator and day from user input in Python. I have a pandas DataFrame with applicator information ('DeviceName') and day of the month ('DAY'). I'm getting the unique day and device name from the DataFrame with:

devices = df['DeviceName'].unique()
days = df['DAY'].unique()

I'm getting the herbicide usage from the user with:

herbicides = []

for device in devices:
    for day in days:
        herbicide = input("How much herbicide did " + str(device) + " use on Day " + str(day) + "?: ")
        herbicides.append(herbicide)

If possible, I would like the final output to display the user input like this:

   DeviceName          24       25       26       27       28
Applied_AA-19     Input #  Input #  Input #  Input #  Input #
Applied_AA-24     Input #  Input #  Input #  Input #  Input #

答案1

得分: 1

IIUC,您可以使用输入数据来初始化一个字典,然后将其传递给DataFrame构造函数:

devices, days = df["DeviceName"].unique(), df["DAY"].unique()

d = {
    device: {
        day: input(f"How much herbicide did {device} use on Day {day}?: ")
        for day in days
    }
    for device in devices
}

out = pd.DataFrame.from_dict(d, orient="index").reset_index(names="DeviceName")

输出:

How much herbicide did Applied_AA-19 use on Day 24?:  1
How much herbicide did Applied_AA-19 use on Day 25?:  2
How much herbicide did Applied_AA-19 use on Day 26?:  3
How much herbicide did Applied_AA-19 use on Day 27?:  4
How much herbicide did Applied_AA-19 use on Day 28?:  5
How much herbicide did Applied_AA-24 use on Day 24?:  6
How much herbicide did Applied_AA-24 use on Day 25?:  7
How much herbicide did Applied_AA-24 use on Day 26?:  8
How much herbicide did Applied_AA-24 use on Day 27?:  9
How much herbicide did Applied_AA-24 use on Day 28?:  10

print(out)

      DeviceName 24 25 26 27  28
0  Applied_AA-19  1  2  3  4   5
1  Applied_AA-24  6  7  8  9  10

使用的输入数据:

df = pd.DataFrame({
    "DeviceName": ["Applied_AA-19", "Applied_AA-24"]*4,
    "DAY": [24, 25, 25, 26, 26, 27, 28, 28]
})
英文:

IIUC, you can bootstrap a dictionnary with the inputs and pass it to the DataFrame constructor :

devices, days = df["DeviceName"].unique(), df["DAY"].unique()

d = {
    device: {
        day: input(f"How much herbicide did {device} use on Day {day}?: ")
        for day in days
    }
    for device in devices
}

out = pd.DataFrame.from_dict(d, orient="index").reset_index(names="DeviceName")

Output :

How much herbicide did Applied_AA-19 use on Day 24?:  1
How much herbicide did Applied_AA-19 use on Day 25?:  2
How much herbicide did Applied_AA-19 use on Day 26?:  3
How much herbicide did Applied_AA-19 use on Day 27?:  4
How much herbicide did Applied_AA-19 use on Day 28?:  5
How much herbicide did Applied_AA-24 use on Day 24?:  6
How much herbicide did Applied_AA-24 use on Day 25?:  7
How much herbicide did Applied_AA-24 use on Day 26?:  8
How much herbicide did Applied_AA-24 use on Day 27?:  9
How much herbicide did Applied_AA-24 use on Day 28?:  10

print(out)

      DeviceName 24 25 26 27  28
0  Applied_AA-19  1  2  3  4   5
1  Applied_AA-24  6  7  8  9  10

Input used :

df = pd.DataFrame({
    "DeviceName": ["Applied_AA-19", "Applied_AA-24"]*4,
    "DAY": [24, 25, 25, 26, 26, 27, 28, 28]
})

huangapple
  • 本文由 发表于 2023年5月23日 01:31:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定