当包含 1 或 2 个其他命名属性时,在 Python 中显示 csv 文件的属性

发布于 2025-01-15 00:34:11 字数 326 浏览 3 评论 0 原文

我是 python 新手,非常感谢您的帮助。我在 Excel 中创建了一个汽车属性文件,并将其另存为 csv 文件,名为 cars.csv,如下所示:

Car make, colour, price, number of seats, automatic, petrol
Ford, black, 40000,5,yes,no
Tesla, white, 90000,4,yes,no

在标题之后,我有 20 行包含不同的汽车及其属性。

有人可以帮助我用 python 代码返回所有具有 4 个座位的汽车品牌,或者说成本 40000,或者说这两个属性吗?谢谢。

I'm new to python and would really appreciate some help please. I've created a file of car attributes in Excel and saved it as a csv file, called cars.csv like this:

Car make, colour, price, number of seats, automatic, petrol
Ford, black, 40000,5,yes,no
Tesla, white, 90000,4,yes,no

After the headings, I have 20 lines with different cars and their attributes.

Could someone help me with the python code which returns all the makes of cars which have say 4 seats, or say cost 40000, or say both these attributes? Thankyou.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与君绝 2025-01-22 00:34:11

您可以使用Pandas

# pip install pandas
import pandas as pd

df = pd.read_csv('cars.csv', skipinitialspace=True)
print(df)

# Output
  Car make colour  price  number of seats automatic petrol
0     Ford  black  40000                5       yes     no
1    Tesla  white  90000                4       yes     no

过滤您的数据框

out = df[(df['number of seats'] == 4) | (df['price'] == 40000)]
print(out)

# Output
  Car make colour  price  number of seats automatic petrol
0     Ford  black  40000                5       yes     no
1    Tesla  white  90000                4       yes     no

您还可以阅读

You can use Pandas:

# pip install pandas
import pandas as pd

df = pd.read_csv('cars.csv', skipinitialspace=True)
print(df)

# Output
  Car make colour  price  number of seats automatic petrol
0     Ford  black  40000                5       yes     no
1    Tesla  white  90000                4       yes     no

Filter your dataframe

out = df[(df['number of seats'] == 4) | (df['price'] == 40000)]
print(out)

# Output
  Car make colour  price  number of seats automatic petrol
0     Ford  black  40000                5       yes     no
1    Tesla  white  90000                4       yes     no

You can also read this

可是我不能没有你 2025-01-22 00:34:11

如果您不想使用任何库,可以使用此方法

# The return value of this method can be handled like a list, but its called a generator object
# This method loads all cars from the csv file and yield this data
def load_cars():
    with open('Cars.csv', 'r') as f:
        for counter, line in enumerate(f):
            if counter > 0:# I skipped the header row
                line_content = line.split(", ")
                producer, colour, price, number_of_seats, automatic, petrol = line_content
                yield producer, colour, price, number_of_seats, automatic, petrol # This is like one item in the list

# The statement below is called a list comprehension
# the load_cars method returns the list like object and it iterates over each object, which is put in car
# The car is put in the list if the condition after the if keyword is true
# Each car which fulfills the condition is in the new created list
filtered_cars = [car_data for car_data in load_cars() if car_data[3] == "4" and car_data[2]=="40000"]
filtered_car_names = [car_data[0] for car_data in filtered_cars]

If you dont want to use any library you can use this approach

# The return value of this method can be handled like a list, but its called a generator object
# This method loads all cars from the csv file and yield this data
def load_cars():
    with open('Cars.csv', 'r') as f:
        for counter, line in enumerate(f):
            if counter > 0:# I skipped the header row
                line_content = line.split(", ")
                producer, colour, price, number_of_seats, automatic, petrol = line_content
                yield producer, colour, price, number_of_seats, automatic, petrol # This is like one item in the list

# The statement below is called a list comprehension
# the load_cars method returns the list like object and it iterates over each object, which is put in car
# The car is put in the list if the condition after the if keyword is true
# Each car which fulfills the condition is in the new created list
filtered_cars = [car_data for car_data in load_cars() if car_data[3] == "4" and car_data[2]=="40000"]
filtered_car_names = [car_data[0] for car_data in filtered_cars]
季末如歌 2025-01-22 00:34:11

您应该在这里使用 pandas.loc[] 函数。如果使用 pandas 加载 csv,则可以使用 loc 函数仅选择符合条件的行。

import pandas as pd

# if index was dropped when saving the csv
car_atts = pd.read_csv('path to file as string')

# if index was not dropped
car_atts = pd.read_csv('path to file as string', index_col=0)

4_seater_cars = car_atts.loc[car_atts['number of seats'] == 4]

# if col name is type string
fourty_k_cars = car_atts.loc[car_atts['40000'] == True]

# if col name is type int
fourty_k_cars = car_atts.loc[car_atts[40000] == True]

# you can use the & (AND) to find rows that match both conditions
4_seater_fourty_k_cars = car_atts.loc[
    (car_atts['number of seats'] == 4) &
    (car_atts['40000'] == True)
]

# you can use the | (OR) to find rows that match either condition
4_seater_fourty_k_cars = car_atts.loc[
    (car_atts['number of seats'] == 4) |
    (car_atts['40000'] == True)
]

希望这能回答您的问题。

快乐编码!!

You should use the pandas.loc[] function here. If you load the csv with pandas, you can use the loc function to select only the rows that match your conditions.

import pandas as pd

# if index was dropped when saving the csv
car_atts = pd.read_csv('path to file as string')

# if index was not dropped
car_atts = pd.read_csv('path to file as string', index_col=0)

4_seater_cars = car_atts.loc[car_atts['number of seats'] == 4]

# if col name is type string
fourty_k_cars = car_atts.loc[car_atts['40000'] == True]

# if col name is type int
fourty_k_cars = car_atts.loc[car_atts[40000] == True]

# you can use the & (AND) to find rows that match both conditions
4_seater_fourty_k_cars = car_atts.loc[
    (car_atts['number of seats'] == 4) &
    (car_atts['40000'] == True)
]

# you can use the | (OR) to find rows that match either condition
4_seater_fourty_k_cars = car_atts.loc[
    (car_atts['number of seats'] == 4) |
    (car_atts['40000'] == True)
]

Hope this answers your question.

Happy coding!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文