从pandas数据框中的每个单元格中删除列表 [英] Removing lists from each cell in pandas dataframe
问题描述
我有一个数据框,其中包含许多单个单元格中的列表.有些单元格没有列表,而只是字符串,而有些只是整数或数字.
I have one dataframe that contains lists in many of the individual cells. Some cells do not have lists and are just strings and some are just integers or numbers.
我想摆脱数据框中的所有列表(当然保留列表中的值或字符串).我该怎么办?
I would like to get rid of all lists in the dataframe (keeping the value or string that was in the list of course). How would I go about this?
下面是两个数据框,一个是原始数据",其中始终包含列表,数字和字符串.第二个是我希望创建的干净数据.
Below are two dataframes, one is the "raw data" which has lists and numbers and strings throughout. The second is the clean data that I am hoping to create.
最简单,最有效的方法是什么?
What is the simplest and most efficient way to do this?
import pandas as pd
#create two dataframes, one called raw, one called end result
#raw data
raw_data = {'Name': [['W1'], ['W3'], ['W2'], ['W1'], ['W2'],['W3'],['G1']],
'EVENT':['E1', 'E2', 'E3', 'E4', 'E5','E6','E1'],
'DrillDate': [['01/01/2000'], 23, '04/01/2000', ['05/15/2000'], [''],[''],'02/02/2000']}
dfRaw = pd.DataFrame(raw_data, columns = ['Name','EVENT','DrillDate'])
dfRaw
# cleaned data
clean_data = {'Name': ['W1', 'W3', 'W2', 'W1', 'W2','W3','G1'],
'EVENT':['E1', 'E2', 'E3', 'E4', 'E5','E6','E1'],
'DrillDate': ['01/01/2000', 23, '04/01/2000', '05/15/2000', '','','02/02/2000']}
dfEndResult = pd.DataFrame(clean_data, columns = ['Name','EVENT','DrillDate'])
dfEndResult
推荐答案
使用applymap
,并在单元格值上使用isinstance
检查类型.
Using, applymap
and check the type using isinstance
on cell values.
In [666]: dfRaw.applymap(lambda x: x[0] if isinstance(x, list) else x)
Out[666]:
Name EVENT DrillDate
0 W1 E1 01/01/2000
1 W3 E2 23
2 W2 E3 04/01/2000
3 W1 E4 05/15/2000
4 W2 E5
5 W3 E6
6 G1 E1 02/02/2000
更新,如果您有空列表,并希望输出空白字符串.
Update, if you've empty lists and want blank string output.
In [689]: dfRaw.applymap(lambda x: x if not isinstance(x, list) else x[0] if len(x) else '')
Out[689]:
Name EVENT DrillDate
0 W1 E1 01/01/2000
1 W3 E2 23
2 W2 E3 04/01/2000
3 W1 E4 05/15/2000
4 W2 E5
5 W3 E6
6 G1 E1 02/02/2000
这篇关于从pandas数据框中的每个单元格中删除列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!