我确信这是一个简单的解决方案,我能得到一点帮助吗?我在这里和网上都找过了,也没能解决这个问题。我刚开始学习还不到一周,基本上已经给自己设置了一个小任务来学习一些基础知识。
import pandas as pd
df1 = pd.read_csv('booking.csv', names=['snum','booked','name'])
df1.drop(['booked', 'name'], axis=1, inplace=True)
df2 = df1.values.tolist()
print('The following tickets are available; %s' % df2)
tic = input('Which ticket would you like to buy? ')
if tic in df2:
print('Ok')
else:
print('Ticket Unavilable')
我遇到的问题是If语句。无论我使用什么值作为输入,我总是得到消息'Ticket Unavailable‘。我相信错误一定出在从数据帧转换过来的列表上。
到目前为止,我已经做到了;
相同
这些变量是基本座位号A1、A2、A3、A4、A5、B1、B2、B3、B4、B5。我知道输入'A‘'B’ect也会返回'OK',实用性不如功能性重要。
发布于 2018-06-10 04:03:08
看起来pandas.DataFrame.values.tolist
并没有产生您认为它所做的事情:
In [1]: import pandas as pd
In [2]: df = pd.DataFrame.from_records([dict(a=1), dict(a=2)])
In [3]: df
Out[3]:
a
0 1
1 2
In [4]: df.values.tolist()
Out[4]: [[1], [2]]
在本例中,tic
是一个字符串,而df.values.tolist()
是一个嵌套列表。我想,你想要的是:
df2 = set(df1['snum'])
我使用了一个集合,因为哈希表更适合查找。
发布于 2018-06-10 04:14:52
看起来您没有使用正确的DataFrame
方法来获取票证列表。使用DataFrame.to_dict('records')
简化票证名称的提取。
booking.csv
92747,true,Harry Potter
28479,false,Sherlock Holmes
Python代码
import pandas as pd
# Load ticket list from CSV file
booking_df = pd.read_csv('booking.csv', names=['snum','booked','name'])
# Convert ticket list to a list of built-in Python dictionaries
ticket_list = booking_df.to_dict('records')
# Extract the ticket names from the list of tickets
ticket_name_set = {ticket["name"] for ticket in ticket_list}
print('The following tickets are available; %s' % ticket_name_set)
wanted_ticket_name = input('Which ticket would you like to buy? ')
if wanted_ticket_name in ticket_name_set:
print('Ok')
else:
print('Ticket Unavilable')
输出:
➜ python tickets.py
The following tickets are available; ['Harry Potter', 'Sherlock Holmes']
Which ticket would you like to buy? Harry Potter
Ok
➜ python tickets.py
The following tickets are available; ['Harry Potter', 'Sherlock Holmes']
Which ticket would you like to buy? Hamlet
Ticket Unavilable
发布于 2018-06-10 04:22:10
变化
if tic in df2:
至
if any(tic in s for s in df2):
您有一个列表,但是您试图像访问Panda Dataframe一样访问它。
booking.csv
snum booked name
a1 no
a2 no
a3 no
a4 no
a5 no
a6 no
a7 no
a8 no
a9 no
a10 no
a11 no
a12 no
a13 no
b1 no
b2 no
b3 no
b4 no
b5 no
b6 no
b7 no
b8 no
b9 no
b10 no
b11 no
b12 no
b13 no
修改后的示例
import pandas as pd
df1 = pd.read_csv('booking.csv', names=['snum','booked','name'])
df1.drop(['booked', 'name'], axis=1, inplace=True)
df2 = df1.values.tolist()
print('The following tickets are available; %s' % df2)
tic = input('Which ticket would you like to buy? ')
if any(tic in s for s in df2):#df2.str.contains(tic):
print('Ok')
else:
print('Ticket Unavilable')
给出以下输出
The following tickets are available; [['snum'], ['a1'], ['a2'], ['a3'], ['a4'], ['a5'], ['a6'], ['a7'], ['a8'], ['a9'], ['a10'], ['a11'], ['a12'], ['a13'], ['b1'], ['b2'], ['b3'], ['b4'], ['b5'], ['b6'], ['b7'], ['b8'], ['b9'], ['b10'], ['b11'], ['b12'], ['b13']]
Which ticket would you like to buy? a1
Ok
或
The following tickets are available; [['snum'], ['a1'], ['a2'], ['a3'], ['a4'], ['a5'], ['a6'], ['a7'], ['a8'], ['a9'], ['a10'], ['a11'], ['a12'], ['a13'], ['b1'], ['b2'], ['b3'], ['b4'], ['b5'], ['b6'], ['b7'], ['b8'], ['b9'], ['b10'], ['b11'], ['b12'], ['b13']]
Which ticket would you like to buy? c2
Ticket Unavilable
https://stackoverflow.com/questions/50778026
复制相似问题