【问题标题】:Change row values with certain condition, using row values from json dataset Pandas使用来自 json 数据集 Pandas 的行值在特定条件下更改行值
【发布时间】:2019-04-18 13:05:05
【问题描述】:

我有这个数据集。

{
    "date": "2018-01-01", 
    "body": "some txt", 
    "id": 111, 
    "sentiment": null
}, 
{
    "date": "2018-01-02", 
    "body": "some txt", 
    "id": 112, 
    "sentiment": {
        "basic": "Bearish"
    }
}

我想用 pandas 阅读这篇文章,并将每行的列情绪更改为与 null 不同。

当我这样做时:

pd.read_json(path)

这是我得到的结果:

body           ...    sentiment
0                      None
1                      {u'basic': u'Bullish'}

我不想拥有{u'basic': u'Bullish'},而只想拥有基本的价值。 所以要找到我使用的正确行

df.loc[self.df['sentiment'].isnull() != True, 'sentiment'] = (?)

它有效,但我不知道我必须放什么来代替 (?)

我试过了,但是不行

df.loc[self.df['sentiment'].isnull() != True, 'sentiment'] = df['sentiment']['basic]

有什么想法吗?谢谢

【问题讨论】:

    标签: python pandas dataset


    【解决方案1】:

    你可以试试:

    mask = df['sentiment'].notnull()
    df.loc[mask, 'sentiment'] = df.loc[mask, 'sentiment'].apply(lambda x: x['basic'])
    

    【讨论】:

    • 它有效。谢谢。此答案将被标记为正确。
    【解决方案2】:

    你可以这样做:

    df = pd.read_json(path)  # creates the dataframe with dict objects in sentiment column 
    pd.concat([df.drop(['sentiment'], axis=1), df['sentiment'].apply(pd.Series)], axis=1)  # create new columns for each sentiment type
    

    例如,如果你的 json 是:

    [{
        "date": "2018-01-01", 
        "body": "some txt", 
        "id": 111, 
        "sentiment": null
    }, 
    {
        "date": "2018-01-02", 
        "body": "some txt", 
        "id": 112, 
        "sentiment": {
            "basic": "Bearish"
        }
    },
    {
        "date": "2018-01-03", 
        "body": "some other txt", 
        "id": 113, 
        "sentiment": {
            "basic" : "Bullish",
            "non_basic" : "Bearish"
        }
    }]
    

    第 1 行之后的df:

                 body       date   id                                     sentiment
    0        some txt 2018-01-01  111                                          None
    1        some txt 2018-01-02  112                          {'basic': 'Bearish'}
    2  some other txt 2018-01-03  113  {'basic': 'Bullish', 'non_basic': 'Bearish'}
    

    第 2 行之后的df:

                 body       date   id    basic non_basic
    0        some txt 2018-01-01  111      NaN       NaN
    1        some txt 2018-01-02  112  Bearish       NaN
    2  some other txt 2018-01-03  113  Bullish   Bearish
    

    HTH。

    【讨论】:

      【解决方案3】:

      fillna + pop + join

      这是一个可扩展的解决方案,它避免了逐行 apply 并将任意数量的键转换为系列:

      df = pd.DataFrame({'body': [0, 1],
                         'sentiment': [None, {u'basic': u'Bullish'}]})
      
      df['sentiment'] = df['sentiment'].fillna(pd.Series([{}]*len(df.index), index=df.index))
      
      df = df.join(pd.DataFrame(df.pop('sentiment').values.tolist()))
      
      print(df)
      
         body    basic
      0     0      NaN
      1     1  Bullish
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-31
        • 1970-01-01
        • 2017-07-16
        • 2021-09-05
        • 1970-01-01
        • 2020-02-20
        • 2022-11-24
        • 1970-01-01
        相关资源
        最近更新 更多