Python 3.x python使用用户级输入创建数据帧
我刚刚开始学习pandas,我正在使用数据帧和数据结构进行学习,因此,当我为字典和索引提供硬代码值时,它可以正常工作,但我希望这是基于用户输入的,用户可以将输入和值存储在字典中,并基于此产生预期结果: 下面的示例代码运行良好,其中包含硬代码值 字典和索引 在下面的例子中,我试图以这样的方式构建它,所以 将请求用户输入,并基于该数据创建一个框架 结果是可以避免的,但有些人工作不正常 所以,当我执行它时,它显示了以下结果。。请帮助我了解我需要做些什么才能使它运行到其他序列。。 正如我所定义的变量purchase_1、purchase_2和purchase_3,其中它只pics第一个,而跳过其余的Python 3.x python使用用户级输入创建数据帧,python-3.x,pandas,Python 3.x,Pandas,我刚刚开始学习pandas,我正在使用数据帧和数据结构进行学习,因此,当我为字典和索引提供硬代码值时,它可以正常工作,但我希望这是基于用户输入的,用户可以将输入和值存储在字典中,并基于此产生预期结果: 下面的示例代码运行良好,其中包含硬代码值 字典和索引 在下面的例子中,我试图以这样的方式构建它,所以 将请求用户输入,并基于该数据创建一个框架 结果是可以避免的,但有些人工作不正常 所以,当我执行它时,它显示了以下结果。。请帮助我了解我需要做些什么才能使它运行到其他序列。。 正如我所定义的变量pu
在您给出的最后一个示例中,
purchae_1
中有不同的列名。您使用成本
两次。和项目成本
一次。在purchae_1
中,将项目成本
更改为成本
和项目采购
更改为项目采购
本质上,发生此问题是因为列名不同。一个非常简单的修复
import pandas as pd
import numpy as np
User_Name = input('Name ')
Item_Purchased = input('Item Purchased ')
Item_Cost = input('Cost ')
purchae_1 = pd.Series( {'Name ': User_Name,
'Item Purchased ' : Item_Purchased, #<--- change is here
'Cost ' : Item_Cost}) #<--- change is here
purchae_2 = pd.Series({'Name ': User_Name,
'Item Purchased ': Item_Purchased,
'Cost ': Item_Cost})
purchae_3 = pd.Series({'Name ': User_Name,
'Item Purchased ': Item_Purchased,
'Cost ': Item_Cost})
df = pd.DataFrame([purchae_1,purchae_2,purchae_3], index = ['Store1', 'Store2', 'Store3'])
print(df.head())
将熊猫作为pd导入
将numpy作为np导入
User\u Name=输入('名称')
采购项目=输入(“采购项目”)
项目成本=输入(“成本”)
purchae_1=pd.Series({'Name':用户名,
“购买的物品”:购买的物品,#我认为您的问题来自标签错误。
有时使用“成本”,有时使用“项目成本”;对于“项目采购”和“项目采购”使用相同的方法
如果您继续使用相同的标签,则应该可以:
purchae_1 = pd.Series( {'Name ': User_Name,
'Item Purchased ' : Item_Purchased,
'Cost ' : Item_Cost})
purchae_2 = pd.Series({'Name ': User_Name,
'Item Purchased ': Item_Purchased,
'Cost ': Item_Cost})
purchae_3 = pd.Series({'Name ': User_Name,
'Item Purchased ': Item_Purchased,
'Cost ': Item_Cost})
@Karn,你应该可以复制并粘贴我上面的例子:)MattR..让我试试看,当然我的错,我只是复制了键值位置上的相同数据:(Yed,MattR Thnx点击大复选标记可以随意接受答案。这让其他人知道你的问题已经得到了回答!编码快乐
bash-4.1$ ./pythonDatafram.py
Name Karn
Item Purchased Dog Food
Cost 22.50
Cost Item Purchased Item_Cost Item_Purchased Name
Store1 NaN NaN 22.50 Dog Food Karn
Store2 22.50 Dog Food NaN NaN Karn
Store3 22.50 Dog Food NaN NaN Karn
bash-4.1$
import pandas as pd
import numpy as np
User_Name = input('Name ')
Item_Purchased = input('Item Purchased ')
Item_Cost = input('Cost ')
purchae_1 = pd.Series( {'Name ': User_Name,
'Item Purchased ' : Item_Purchased, #<--- change is here
'Cost ' : Item_Cost}) #<--- change is here
purchae_2 = pd.Series({'Name ': User_Name,
'Item Purchased ': Item_Purchased,
'Cost ': Item_Cost})
purchae_3 = pd.Series({'Name ': User_Name,
'Item Purchased ': Item_Purchased,
'Cost ': Item_Cost})
df = pd.DataFrame([purchae_1,purchae_2,purchae_3], index = ['Store1', 'Store2', 'Store3'])
print(df.head())
purchae_1 = pd.Series( {'Name ': User_Name,
'Item Purchased ' : Item_Purchased,
'Cost ' : Item_Cost})
purchae_2 = pd.Series({'Name ': User_Name,
'Item Purchased ': Item_Purchased,
'Cost ': Item_Cost})
purchae_3 = pd.Series({'Name ': User_Name,
'Item Purchased ': Item_Purchased,
'Cost ': Item_Cost})