对列中的所有值调用函数,pandas

问题描述 投票:0回答:1

我正在尝试跨特定列进行(相当补救的)函数调用,显然是为了跨所有索引行调用它。但由于某种原因,输出是索引的重复,该索引不是数据帧中的第一行或最后一行。

功能:

def split_and_order(string):
    return string_samp.split(",")

致电:

sorted_list = [split_and_order(j) for j in michelin["FacilitiesAndServices"]]

输出:

[['Air conditioning',
  'Car park',
  'Interesting wine list',
  'Restaurant offering vegetarian menus',
  'Wheelchair access'],
 ['Air conditioning',
  'Car park',
  'Interesting wine list',
  'Restaurant offering vegetarian menus',
  'Wheelchair access'],
 ['Air conditioning',
  'Car park',
  'Interesting wine list',
  'Restaurant offering vegetarian menus',
  'Wheelchair access'],
 ['Air conditioning',
  'Car park',
  'Interesting wine list',
  'Restaurant offering vegetarian menus',
  'Wheelchair access'],
 ['Air conditioning',
  'Car park',
  'Interesting wine list',
  'Restaurant offering vegetarian menus',
  'Wheelchair access']]

实际数据:

0                   Air conditioning,Wheelchair access
1          Air conditioning,Car park,Wheelchair access
2          Air conditioning,Car park,Wheelchair access
3    Air conditioning,Car park,Garden or park,Inter...
4    Air conditioning,Interesting wine list,Wheelch...
5    Air conditioning,Interesting wine list,Restaur...
Name: FacilitiesAndServices, dtype: object

带来智慧...在 pandas 数据框中的所有值中调用函数的正确方法是什么?这需要 lambda 函数吗?

python pandas
1个回答
0
投票

试试这个:

df['FacilitiesAndServices'] = df['FacilitiesAndServices'].str.split(',', expand=False)
© www.soinside.com 2019 - 2024. All rights reserved.