我刚刚开始学习pandas
我正在使用Dataframe
和数据结构进行学习,所以当我为Dictionary和Index提供硬编码值时,它工作正常,但我想要这个用户输入基于用户可以输入的输入,那些vaue可以存储在Dictionary上,并且基于它可以产生预期的结果:
以下示例代码在其中的硬编码值运行良好 字典&索引..
import pandas as pd
import numpy as np
########### computation by numpy vectorisation method #######
purchae_1 = pd.Series({'Name': 'Karn',
'Item Purchased': 'Dog Food',
'Cost': 22.50})
purchae_2 = pd.Series({'Name': 'Renu',
'Item Purchased': 'Kitty Letter',
'Cost': 2.50})
purchae_3 = pd.Series({'Name': 'Rigved',
'Item Purchased': 'Foot Ball',
'Cost': 12.50})
#df = pd.DataFrame([purchae_1,purchae_2,purchae_3], index = ['Store1', 'Store2', 'Store3'])
df = pd.DataFrame([purchae_1,purchae_2,purchae_3], index = ['Store1', 'Store2', 'Store3'])
print(df.head())
bash-4.1$ ./pythonDatafram.py
Cost Item Purchased Name
Store1 22.5 Dog Food Karn
Store2 2.5 Kitty Letter Renu
Store3 12.5 Foot Ball Rigved
虽然在下面的例子中,我试图以这样的方式构建它,所以,它 将询问用户的输入并基于该Dtaframe将被创建 结果可以是yeilded,但有人不能正常工作
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,
'Item_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})
df = pd.DataFrame([purchae_1,purchae_2,purchae_3], index = ['Store1', 'Store2', 'Store3'])
print(df.head())
所以,当我执行它时,它显示以下结果..请帮助我理解我需要做什么才能让它运行其他序列... 因为我已经定义了Variable purchase_1,buy_2& purchase_3只有pics第一个并跳过其余的......
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$
答案 0 :(得分:2)
在您给出的最后一个示例中,purchae_1
中有不同的列名。您使用Cost
两次。和Item_Cost
一次。在purchae_1
中将Item_Cost
更改为Cost
和Item_Purchased
更改为Item Purchased
实质上,此问题正在发生,因为列名称不同。一个非常简单的修复!
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())
答案 1 :(得分:1)
我认为你的问题来自错误标签。 有时您使用'成本',有时使用'Item_Cost'; 'Item_Purchased'和'Item Purchased'
同样的事情如果你继续使用相同的标签,它应该有效:
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})