简体   繁体   English

列表格式的熊猫数据框列值

[英]panda dataframe column values in list format

For some reason, one of my dataframe column is filled with lists as its value. 由于某种原因,我的dataframe列之一中填充了列表作为其值。

(ex): "Book Date" - [01/20/2018]

I've tried to make it into a string by applying both str(x) and ''.join(x) method but it doesn't work. 我试图通过同时应用str(x)和''.join(x)方法将其转换为字符串,但是它不起作用。

df['Book Date'] = df['Book Date'].map(lambda x: ''.join(x))

Could you please let me know what I should do and how to avoid this in the first place? 您能否先让我知道我应该怎么做以及如何避免这种情况?
Thank you in advance! 先感谢您!

Use apply + join() and map() in order to convert your list to strings first. 使用apply + join()和map()可以先将列表转换为字符串。

Example DataFrame: 示例数据框:

  >>> df1
      Book Date
1  [01/20/2018]
2  [01/22/2018]

Result: 结果:

>>> df1['Book Date'].apply(lambda x: ','.join(map(str, x)))
1    01/20/2018
2    01/22/2018
Name: Book Date, dtype: object

OR 要么

You could convert your list to str with astype(str) and then use replace to remove [ ] & ' . 您可以将列表转换为带有astype(str) ,然后使用replace删除[ ]'

>>> df1['Book Date'].astype(str).str.replace('\[|\]|\'', '')
1    01/20/2018
2    01/22/2018
Name: Book Date, dtype: object

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

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