如何在使用panda和scv搜索值后打印整行



你好,我的投影遇到了一些问题,我正在尝试做一些事情,你可以搜索一个值,它会返回一个值所在的行。我只能用原子序数来做这件事,因为它们是有序的。我本来打算使用类,但我不认为我真的需要它(这就是import元素的作用(

这是我的代码:

from __init__ import element
import csv
import pandas
import elements
while True:
# Search Options
print("Search options: ")
print("Search by Atomic Symbol")
print("Search by Atomic Number")
print("Search by Atomic Weight")
print("Search by English Name")
print("Search by Dutch Name")
searchOption = input().lower()
if searchOption == "atomic number":
# Search by atomic number
atNum = int(input("Atomic number: "))
df = pandas.read_csv("elements.csv", index_col="atNum")
print(f"Atomic Symbol: {df.iloc[atNum - 1].atSym}")
print(f"Atomic Weight: {df.iloc[atNum - 1].atWeight}")
print(f"English Name:  {df.iloc[atNum - 1].ENname}")
print(f"Dutch Name:    {df.iloc[atNum - 1].NLname}")

print("=====================================")

这是我的CSV文件的示例

atSym,atNum,atWeight,ENname,NLname
H,1,1.008,hydrogen,waterstof
He,2,4.0026,helium,helium
Li,3,6.94,lithium,lithium
Be,4,9.0122,beryllium,beryllium
B,5,10.81,boron,boor
C,6,12.011,carbon,koolstof
N,7,14.007,nitrogen,stikstof
O,8,15.999,oxigen,zuurstof
F,9,18.998,fluorine,fluor
Ne,10,20.180,neon,neon
Na,11,22.990,sodium,natrium
Mg,12,24.305,magnesium,magnesium
Al,13,26.982,aluminium,aluminium
Si,14,28.085,silicon,silicium
P,15,30.974,phosphorus,fosfor
S,16,32.06,sulfur,zwavel
Cl,17,35.45,chlorine,chloor
Ar,18,39.948,argon,argon
K,19,39.098,potassium,kalium
Ca,20,40.078,calcium,calcium
Sc,21,44.956,scandium,scandium
Ti,22,47.867,titanium,titanium
V,23,50.942,vanadium,vanadium
Cr,24,51.996,chromium,chroom
Mn,25,54.938,manganese,mangaan
Fe,26,55.845,iron,ijzer
Co,27,58.933,cobalt,kobalt
Ni,28,58.693,nickel,nikkel
Cu,29,63.546,copper,koper

以下是您可以搜索不同选项的方法:

# df is not changing, so no need to read it on every iteration
df = pandas.read_csv("elements.csv")
# mapping between search options and columns in the DataFrame
d = {
'atomic symbol': 'atSym',
'atomic number': 'atNum',
'atomic weight': 'atWeight',
'english name': 'ENname',
'dutch name': 'NLname',
}
while True:
# Search Options
print("Search options: ")
print("Search by Atomic Symbol")
print("Search by Atomic Number")
print("Search by Atomic Weight")
print("Search by English Name")
print("Search by Dutch Name")
# get search option and select the right column to search on
# we're converting the column to lowercase strings, so that
# we can conveniently find it later
searchOption = input().lower()
df_ix = df[d[searchOption]].astype(str).str.lower()

# get value to search for, convert to lowercase and find row
searchValue = input().lower()
row = df[df_ix==searchValue]

# rename columns to human-readable based on our mapping and print
print("=====================================")
print(row.rename(columns={v: k for k, v in d.items()}))
print("=====================================")

示例输出:

Search options: 
Search by Atomic Symbol
Search by Atomic Number
Search by Atomic Weight
Search by English Name
Search by Dutch Name
english name
helium
=====================================
atomic symbol  atomic number  atomic weight english name dutch name
1  He             2             4.0            helium       helium   
=====================================

我解决了这个问题,在Windows中,路径需要为elementify/elementsList.csv,在Mac上,elementsList.csv也可以,我现在发布了它https://github.com/shibaismyname/elementify如果你想看看,我仍在努力。

相关内容

  • 没有找到相关文章

最新更新