Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从列中删除大括号和word_Python_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python 从列中删除大括号和word

Python 从列中删除大括号和word,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我有这个问题,我想从数据框的列中删除大括号和一个单词。 这是称为“权重”的列: Weight {'weight': 24} {'weight': 24} {'weight': 22} {'weight': 17} {'weight': 17} {'weight': 11} {'weight': 21} {'weight': 16} .... 我想要的是: Weight 24 24 22 17 17 11 21 16 .... 这怎

我有这个问题,我想从数据框的列中删除大括号和一个单词。 这是称为“权重”的列:

  Weight
{'weight': 24}
{'weight': 24}
{'weight': 22}
{'weight': 17}
{'weight': 17}
{'weight': 11}
{'weight': 21}
{'weight': 16}
....
我想要的是:

  Weight
   24
   24
   22
   17
   17
   11
   21
   16
   ....

这怎么可能呢?谢谢

我想你有字典{'weight':24},而不是字符串“{'weight':24}”


也许这很奇怪,但是你可以使用
str
来实现这一点

df['Weight'] = df['Weight'].str['weight']

df['Weight'] = df['Weight'].str.get('weight')
您还可以将
apply()
lambda一起使用

df['Weight'] = df['Weight'].apply(lambda item: item['weight'])

df['Weight'] = df['Weight'].apply(lambda item: item.get('weight'))

最小工作代码

import pandas as pd

df = pd.DataFrame({'Weight':[
    {'weight': 24},
    {'weight': 24},
    {'weight': 22},
    {'weight': 17},
    {'weight': 17},
    {'weight': 11},
    {'weight': 21},
    {'weight': 16},
]})

print(df)

df['Weight'] = df['Weight'].str['weight']
#df['Weight'] = df['Weight'].str.get('weight')

#df['Weight'] = df['Weight'].apply(lambda item: item['weight'])
#df['Weight'] = df['Weight'].apply(lambda item: item.get('weight'))

print(df)

编辑:

您还可以将字典转换为
系列
,并获得
权重

df['Weight'] = df['Weight'].apply(pd.Series).get('weight')

df['Weight'] = df['Weight'].apply(pd.Series).explode('weight')

我假设您有字典
{'weight':24}
,而不是字符串
“{'weight':24}”


也许这很奇怪,但是你可以使用
str
来实现这一点

df['Weight'] = df['Weight'].str['weight']

df['Weight'] = df['Weight'].str.get('weight')
您还可以将
apply()
lambda一起使用

df['Weight'] = df['Weight'].apply(lambda item: item['weight'])

df['Weight'] = df['Weight'].apply(lambda item: item.get('weight'))

最小工作代码

import pandas as pd

df = pd.DataFrame({'Weight':[
    {'weight': 24},
    {'weight': 24},
    {'weight': 22},
    {'weight': 17},
    {'weight': 17},
    {'weight': 11},
    {'weight': 21},
    {'weight': 16},
]})

print(df)

df['Weight'] = df['Weight'].str['weight']
#df['Weight'] = df['Weight'].str.get('weight')

#df['Weight'] = df['Weight'].apply(lambda item: item['weight'])
#df['Weight'] = df['Weight'].apply(lambda item: item.get('weight'))

print(df)

编辑:

您还可以将字典转换为
系列
,并获得
权重

df['Weight'] = df['Weight'].apply(pd.Series).get('weight')

df['Weight'] = df['Weight'].apply(pd.Series).explode('weight')

请发布您迄今为止尝试过的任何代码。
[row.get(“weight”)用于df[“weight”]
,如果您的列实际上是字典,请发布您迄今为止尝试过的任何代码。
[row.get(“weight”)用于df[“weight”]
,如果您的列实际上是字典。