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

标签 python-3.x pandas

我刚开始学习 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/

相关文章:

python - 打印整数或带 n 位小数的 float

python - Kmeans 算法的特征缩放

python-3.x - Pandas 列中的 min() 函数

python - 使用 pandas 读入 csv 文件时有关闭列的问题

python - 联合超过 2 个 Pandas 数据框

python-3.x - HTTP 错误 403 Forbidden - 下载 nltk 数据时

python - 使用不带 `key` 的 `reverse=True` 参数按降序对字符串列表进行排序?

python - 使用 pandas dataframes data python 创建堆叠直方图

python - 你将如何优化这个简短但非常慢的 Python 循环?

python - Pandas groupby - 计算与相对点的距离