为什么我的代码没有过滤,仍然打印列中的所有内容,而不是它应该过滤的内容?

2024-04-19 00:33:09 发布

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

这是我在这里的第一篇帖子,所以请原谅我的帖子中的任何错误。你知道吗

这是我的密码。我试图过滤和打印这些数据,但是当脚本运行时,它会打印所有的列标题,这是我不想要的。有什么建议吗?你知道吗

import pandas as pd


data = pd.read_csv("/Users/andrewschaper/Desktop/EQR_Data/EQR_Transactions_1.csv", low_memory=False)
print("Total rows: {0}".format(len(data)))
print(list(data))

df = pd.DataFrame(data)
df.sort_values(by=['Filing_Quarter','product_name','time_zone','increment_name'],ascending=[True, True, True, True])

df.filter(items=['RECID', 'transaction_unique_id', 'ferc_tariff_reference', 'contract_service_agreement', 'transaction_unique_identifier', 'transaction_begin_date', 'transaction_end_date', 'trade_date', 'exchange_brokerage_service', 'point_of_delivery_specific_location', 'class_name', 'term_name', 'increment_peaking_name'])

print(df)

Tags: csvnametruedfdatadate错误service
1条回答
网友
1楼 · 发布于 2024-04-19 00:33:09

你应该把你的代码复制粘贴到这里,但我认为这样可以:

# List of columns you want.
items = [x,y,z]

# Print the filtered dataframe.
df[items]

请记住,df[items]只打印过滤后的数据帧,不会删除其他列。如果这就是你想要的,那么filtered_df = df[items]。你知道吗

相关问题 更多 >