[英]Python Pandas Import Excel sheet cell as object without quotes in the end
My example excel sheet looks like this:我的示例 excel 工作表如下所示:
Excel sheet data: Excel单资料:
customer1_data.xlsx = customer1_data.xlsx =
parameter customer1
analysis 1
analysis_name 1month_services
analysis_duration [2022-08-23, 2022-11-02]
analysis_numcheck 1
analysis_dupcolumns 1
Import excel sheet data as dataframe It looks normal but when I query individual rows or cells, some cell values have quotes at the end.将 excel 工作表数据导入为 dataframe它看起来很正常,但是当我查询单个行或单元格时,某些单元格值在末尾有引号。 I don't want any quotes in the end.我不想在最后引用任何引号。
c1df = pd.read_excel('customer1_data.xlsx')
c1df.set_index('parameter',inplace=True)
print(c1df)
parameter customer1
analysis 1
analysis_name 1month_services
analysis_duration [2022-08-23, 2022-11-02]
analysis_numcheck 1
analysis_dupcolumns 1
Present output When I print individual cell values Present output当我打印单个单元格值时
print(c1df.loc['analysis'])
1
print(c1df.loc['analysis_duration'])
'[2022-08-23, 2022-11-02]'
print(c1df.loc['analysis_name'])
'1month_services'
Expected output:预计 output:
print(c1df.loc['analysis'])
1
print(c1df.loc['analysis_duration'])
# I don't want any quotes at the end for the list here
[2022-08-23, 2022-11-02]
print(c1df.loc['analysis_name'])
# ' ' quote is expected for the string, no issues here
'1month_services' '1month_services'
You can use pandas.Series.split
to convert string delimited to lists:您可以使用pandas.Series.split
将分隔的字符串转换为列表:
c1df["customer1"]= (
c1df["customer1"].str.strip("[]")
.str.split(",")
.where(c1df["customer1"].str.contains("[\[\]]", regex=True, na=False))
.fillna(c1df["customer1"])
)
print(c1df)
parameter customer1
0 analysis 1
1 analysis_name 1month_services
2 analysis_duration [2022-08-23, 2022-11-02]
3 analysis_numcheck 1
4 analysis_dupcolumns 1
print(c1df.iloc[2,1])
['2022-08-23', ' 2022-11-02']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.