简体   繁体   English

解析数据框列以创建新的数据框

[英]Parse through dataframe column to create new dataframe

I have a dataframe with colmuns that have nested attribute values pairs. 我有一个带有嵌套属性值对的colmuns的数据框。 How can I parse through this dataframe column and get these value to create a new dataframe for it? 如何解析此dataframe列并获取这些值以为其创建一个新的dataframe?

This is one of the column values look like: 这是列值之一,如下所示:

{'BikeParking': 'False', 'BusinessAcceptsCreditCards': 'True', 'BusinessParking': "{'garage': False, 'street': True, 'validated': False, 'lot': False, 'valet': False}", 'GoodForKids': 'True', 'HasTV': 'True', 'NoiseLevel': 'average', 'OutdoorSeating': 'False', 'RestaurantsAttire': 'casual', 'RestaurantsDelivery': 'False', 'RestaurantsGoodForGroups': 'True', 'RestaurantsPriceRange2': '2', 'RestaurantsReservations': 'True', 'RestaurantsTakeOut': 'True'}

Not all the columns have the same attributes as well. 并非所有列都具有相同的属性。 Please help, I am new in python. 请帮助,我是python新手。

If your dict is not nested, the following will work. 如果您的字典未嵌套,则可以执行以下操作。

pd.DataFrame(list(df['column_name'])

If you have nested dicts, try doing the above again on the nested dicts. 如果您有嵌套词典,请尝试对嵌套词典再次执行上述操作。

It's not very clear what you're looking for but another thing that might help is using the apply function to parse every row of your dataframe to create a new column with the result. 不清楚要查找什么,但可能有帮助的另一件事是使用apply函数来解析数据帧的每一行,以创建带有结果的新列。

def _parsing_function(row):
    // column containing nested values
    dict = row['column_name']
    for key in dict:
        'do something'
        return 'new column value'

frame['new_column'] = frame.apply(_parsing_function, axis=1)

To find the dictionaries inside your existing dataframe column and make new dataframes, you can iterate over your column and create dataframes as needed: 要在现有数据框列中查找字典并创建新的数据框,可以遍历该列并根据需要创建数据框:

new_dfs = []   # list of new dataframes from dictionaries in existing column
for elem in df['column_name']:
    if type(elem) is dict:
        new_dfs.append(pd.DataFrame(elem))  # add to list of created dataframes

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

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