【问题标题】:how to convert excel columns to one list separted by comma in python如何在python中将excel列转换为一个用逗号分隔的列表
【发布时间】:2020-03-09 00:22:46
【问题描述】:

我正在尝试将 excel 列转换为以逗号分隔的数据框列表, Here is the excel sheet

这是我正在尝试的代码:

df=pd.read_excel('eng_test.xlsx',"Sheet1")
new_df = pd.DataFrame(df) 
for column in new_df:
    new_df2=new_df.apply(pd.Series)
    new_df3=(new_df2.to_string())
    new_df4=new_df3.split()
#print(new_df4)

我希望 df 是这样的 df = pd.DataFrame(['This is my first sentence', 'This is a second sentence with more words'])

【问题讨论】:

标签: python excel pandas dataframe


【解决方案1】:

虽然不清楚你究竟是什么来实现具有 Excel 列或列表中的行的 Dataframe,但你可以使用 header=None 执行类似的操作:

df=pd.read_excel('text_excel.xlsx', header=None)
print("\nDataframe: ")
print(df)
df_list = df.values.tolist()
print("\nList: ")
print(df_list)

这样你会得到以下输出:

Dataframe: 
                                           0
0                'This is my first sentence'
1  This is a second sentence with more words

List: 
[["'This is my first sentence'"], ['This is a second sentence with more words']]

【讨论】:

  • 当我的声望达到 15 时肯定会这样做,因为当它低于那个对不起时我不能。但一定会这样做
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
  • 2016-10-24
  • 1970-01-01
  • 1970-01-01
  • 2019-11-20
  • 2019-01-06
  • 2020-04-07
相关资源
最近更新 更多