如何在数据框中的特定行和列中插入输入值

2024-09-19 23:43:55 发布

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

我需要一些帮助

我有一个数据帧,比如:

A B C D
1 2 3 
5 6 7 
5 7 5

我想用python从输入中获取D值:

比如说

for i in df.iterlupes():
    d= input(" put the number:")
    df["d"] = d

例如,我将输入1 2 3,我将有:

A B C D
1 2 3 1
5 6 7 2
5 7 5 3

请问我怎么做?非常感谢


Tags: the数据innumberdfforinputput
2条回答

我希望这就是你要寻找的:

import pandas as pd 
df = pd.DataFrame({"A":[1, 5, 5], 
                   "B":[2, 6, 7],  
                   "C":[3, 7, 5], 
                   "D":[" "]*3})
print (df)
for e in range (3):
    d= input(" put the number:")
    df.at[e,'D']=d
print (df)

输出为:

   A  B  C  D
0  1  2  3   
1  5  6  7   
2  5  7  5   
 put the number:1
 put the number:2
 put the number:3
   A  B  C  D
0  1  2  3  1
1  5  6  7  2
2  5  7  5  3

示例的最终代码(基于注释):

def enter_new_column(df, name): 
    for e in range (df.shape[0]):
        d= input(" put the number: ")
        df.at[e,name]=d
        
enter_new_column(devis, "Quantité") 

我想我知道你在问什么,虽然我不知道.interlupes()是什么意思

您想要做的是相对简单的,您想要捕获用户输入并将它们放置在数据帧中的某个位置。你没有弄清楚的是,你是如何捕捉到它们放在哪里的

在您的示例中,您可以这样做:

import pandas as pd
df = pd.DataFrame({"A":[1,5,5],"B":[2,6,7],"C":[3,7,5]})

def enter_new_column(df, name, print_column):

    # loop over length of the rows
    temp_arr = []
    for i in range(df.shape[0]):
        temp_arr.append(input(f"Put the Number for the value {df[print_column][i]}: "))
    df[name] = temp_arr

enter_new_column(df=df, name="D", print_column="Intitulé_complet")

相关问题 更多 >