gpt4 book ai didi

python-3.x - 具有用户级输入的python pandas Dataframe

转载 作者:行者123 更新时间:2023-12-01 02:42:00 26 4
gpt4 key购买 nike

我刚开始学习 pandas,我正在使用 Dataframe 和数据结构进行学习,所以当我为 Dictionary 和 Index 提供硬编码值时,它工作正常但我希望这是基于用户输入的用户输入,这些输入可以存储在字典中,并基于它可以产生预期的结果:

The below example code runs good with hardcode values within Dictionary & Index ..

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

While in The below example i'm trying to build it in such a way so, it will ask for User's input and based on that Dtaframe will be created and result can be yeilded but someone its not working properly

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())

So, when i executed it , it shows the below results.. please help me to understand what i need to do to make it running for other sequese as well.. as i have defined the Variable purchase_1, purchase_2 & purchase_3 where it only pics First one and skips the rest...

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$

最佳答案

在您给出的最后一个示例中,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())

关于python-3.x - 具有用户级输入的python pandas Dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47203890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com