Python 从 pandas.dataframe 中提取特定列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48641632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:47:24  来源:igfitidea点击:

Extracting specific columns from pandas.dataframe

pythonpandasspyder

提问by Yags

I'm trying to use python to read my csv file extract specific columns to a pandas.dataframeand show that dataframe. However, I don't see the data frame, I receive Series([], dtype: object) as an output. Below is the code that I'm working with: My document consists of: product sub_product issue sub_issue consumer_complaint_narrative
company_public_response company state zipcode tags
consumer_consent_provided submitted_via date_sent_to_company
company_response_to_consumer timely_response consumer_disputed?
complaint_id

我正在尝试使用 python 读取我的 csv 文件,将特定列提取到 apandas.dataframe并显示该数据框。但是,我没有看到数据框,我收到 Series([], dtype: object) 作为输出。以下是我正在使用的代码: 我的文档包括: product sub_product issue sub_issue consumer_complaint_narrative
company_public_response company state zipcode tags
consumer_consent_provided submit_via date_sent_to_company
company_response_to_consumer及时_响应消费者_有争议的?
投诉号码

I want to extract : sub_product issue sub_issue consumer_complaint_narrative

我想提取:sub_product issue sub_issue consumer_complaint_narrative

import pandas as pd

df=pd.read_csv("C:\....\consumer_complaints.csv")
df=df.stack(level=0)
df2 = df.filter(regex='[B-F]')
df[df2]

回答by kepy97

import pandas as pd

input_file = "C:\....\consumer_complaints.csv"
dataset = pd.read_csv(input_file)
df = pd.DataFrame(dataset)
cols = [1,2,3,4]
df = df[df.columns[cols]]

Here specify your column numbers which you want to select. In dataframe, column start from index = 0

在此指定要选择的列号。在数据框中,列从索引 = 0 开始

cols = []

You can select column by name wise also. Just use following line

您也可以按名称选择列。只需使用以下行

df = df[["Column Name","Column Name2"]]

回答by PaW

A simple way to achieve this would be as follows:

实现此目的的简单方法如下:

df = pd.read_csv("C:\....\consumer_complaints.csv")
df2 = df.loc[:,'B':'F']

Hope that helps.

希望有帮助。