从excel中仅选择和导入某些列以进行导入 [英] Selecting and importing only certain columns from excel for importing

查看:147
本文介绍了从excel中仅选择和导入某些列以进行导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

'我有一个Excel文件,其中包含许多带有字符串的列,但我想导入此包含'NGUYEN'的Excel文件中的某些列.

'I have an excel file which contains many columns with strings, but i want to import certain columns of this excel file containing 'NGUYEN'.

我想从Excel中的包含"NGUYEN"的列中生成一个字符串.

I want to generate a string from columns in my excel which had 'NGUYEN' in them.

import pandas as pd
data = pd.read_excel("my_excel.xlsx", parse_cols='NGUYEN' in col for cols in my_excel.xlsx, skiprows=[0])
data = data.to_string()
print(data)


SyntaxError: invalid syntax

my_excel.xlsx

my_excel.xlsx

函数输出应为

data =  'NGUYEN VIETNAM HANOIR HAIR PANTS BIKES CYCLING ORANGE GIRL TABLE DARLYN NGUYEN OMG LOL'   

推荐答案

我很确定这是您要寻找的.如果您需要帮助使可读性更高的多行函数,我尝试过使其尽可能简单和紧凑.让我知道!

I'm pretty sure this is what you are looking for. I tried making it as simple and compact as possible, if you need help making a more readable multi-line function. Let me know!

import pandas as pd
data = pd.read_excel("my_excel.xlsx")
getColumnsByContent = lambda string:  ' '.join([' '.join([elem for elem in data[column]]) for column in data.columns  if string in data[column].to_numpy()])
print(getColumnsByContent('NGUYEN'))
print(getColumnsByContent('PANTS'))

这篇关于从excel中仅选择和导入某些列以进行导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆