如何在pandas中将Excel数据导入为列表?

huangapple go评论66阅读模式
英文:

How to import Excel data in pandas as list?

问题

我的Excel工作表中有一些列,其中之一是类似Python列表的列。如果我使用pandas.read_excel导入这个Excel数据,是否可能让pandas在这个阶段或之后识别该列为列表?我之所以这么问是因为我在Excel中有逗号分隔的值,我想在导入Excel文件后使用pandas的explode()。

我尝试用[""]包装Excel单元格,但导入和展开并未按预期工作。有什么指导吗?

谢谢!

英文:

My Excel worksheet has some columns, one of which is Python list-like column. If I import this Excel data using pandas.read_excel, is it possible for pandas to recognize that column as list at this stage or later? I am asking because I have comma-seperated values residing in Excel and I want to use pandas' explode() after importing the Excel file.

I tried to wrap the Excel cells with [""] but the importing and exploding did not work as desired. Any guidance?

Thanks!

如何在pandas中将Excel数据导入为列表?

data = {
  "Name": ["A", "B", "C","D"],
  "Product Sold": [["Apple", "Banana"], ["Apple", "Pear"], ["Pear"], ["Berry"]],
  "Prices": [[5,6], [5,8], [4], [3]]
}

df = pd.DataFrame(data)
df.explode(['Product Sold', 'Prices'])

答案1

得分: 1

你可以尝试类似这样的方法:

import pandas as pd

data = {
  "Name": "Apple,Pear",
}

df = pd.DataFrame(data, index=[1])
for c in df.columns:
    if df[c].str.contains(','):
        df[c] = df[c].apply(lambda x: str(x).split(','))

print(type(df.Name.iloc[0]))

读取你的Excel文件,然后将其通过上面的for循环处理,它应该会将逗号分隔的单元格转换成列表。

如果有帮助,请告诉我。

英文:

You could try something like this:

import pandas as pd

data = {
  "Name": "Apple,Pear",
}

df = pd.DataFrame(data,index=[1])
for c in pdf.columns:
    if df[c].str.contains(','):
        df[c] = df[c].apply(lambda x : str(x).split(','))
    
print(type(df.Name.iloc[0]))

Read in your excel file, then pass it through the for loop above and it should make lists out of comma-delimited cells.

Let me know if it helps.

huangapple
  • 本文由 发表于 2023年2月16日 05:28:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定