简体   繁体   English

从 pandas.dataframe 中提取特定列

[英]Extracting specific columns from pandas.dataframe

I'm trying to use python to read my csv file extract specific columns to a pandas.dataframe and show that dataframe.我正在尝试使用 python 读取我的 csv 文件将特定列提取到pandas.dataframe并显示该数据帧。 However, I don't see the data frame, I receive Series([], dtype: object) as an output.但是,我没有看到数据框,我收到 Series([], dtype: object) 作为输出。 Below is the code that I'm working with: My document consists of: product sub_product issue sub_issue consumer_complaint_narrative以下是我正在使用的代码:我的文档包括:product sub_product issue sub_issue consumer_complaint_narrative
company_public_response company state zipcode tags company_public_response 公司州邮政编码标签
consumer_consent_provided submitted_via date_sent_to_company consumer_consent_provided submit_via date_sent_to_company
company_response_to_consumer timely_response consumer_disputed? company_response_to_consumer及时_响应consumer_disputed?
complaint_id投诉号码

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]
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"]]

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.希望有帮助。

This worked for me , using slicing:这对我有用,使用切片:

df=pd.read_csv df=pd.read_csv

df1=df[n1:n2] df1=df[n1:n2]

Where $n1<n2# are both columns in the range, eg: if you want columns 3-5, use:其中 $n1<n2# 都是范围内的列,例如:如果您想要第 3-5 列,请使用:

df1=df[3:5] df1=df[3:5]

For the first column , use df1=df[0]对于第一列,使用 df1=df[0]

Though not sure how to select a discontinuous range of columns.虽然不确定如何选择不连续的列范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM