Python:我需要从数据帧中提取某些值并创建一个新的datafram

2024-04-30 02:31:29 发布

您现在位置:Python中文网/ 问答频道 /正文

很抱歉问你,但我是个Python迷,我需要帮助

所以,我有这个csv(https://www.kaggle.com/jtrofe/beer-recipes),我需要提取某些值

我想用相同的列和值创建一个新的数据框,但我想从“Style”列中只提取“American IPA,American Pale Ale,Saison,American Light Lager e American Amber Ale”。并用这个创建一个数据帧

有人能帮我吗

谢谢


Tags: csv数据httpscomstylewwwipaamerican
1条回答
网友
1楼 · 发布于 2024-04-30 02:31:29

我使用^{}索引和布尔级数生成器^{}

import pandas as pd

# Read in the full data set, check its size
original_df = pd.read_csv('recipeData.csv', encoding='latin-1')
print(original_df.size)  # 1698803

# Store your desired styles for filtering in a python list
styles_list = "American IPA, American Pale Ale, Saison, American Light Lager, American Amber Ale".split(', ')

# Filter using .loc and a boolean mask (checking if each 'Style' value is in your list)
new_df = original_df.loc[original_df['Style'].isin(styles_list)]
print(new_df.size)  # 608419

相关问题 更多 >