使用条件值创建新的熊猫数据框值

时间:2019-11-17 03:17:23

标签: python pandas dataframe

我有一个像这样的数据框df

| created_at | favourite_count | hashtags | location | mined_at | name | quote_screen_name | quote_text | retweet_count |                    retweet_text                    | screen_name |    source_device    | status_count |                                            text                                           |   tweet_id  |
|:----------:|:---------------:|:--------:|:--------:|:--------:|:----:|:-----------------:|:----------:|:-------------:|:--------------------------------------------------:|:-----------:|:-------------------:|:------------:|:-----------------------------------------------------------------------------------------:|:-----------:|
|   49:02.0  |        0        |    []    |          |  16:07.4 | Eric |        None       |    None    |       23      | Sound design in this game is 10/10 game freak lied |  narutouz16 |   Twitter Web App   |      364     |              RT @GetMadz: Sound design in this game is 10/10 game freak lied.             | 1.19558E+18 |
|   45:43.0  |        1        |    []    |          |  16:07.4 | Eric |        None       |    None    |       0       |                        None                        |  narutouz16 |   Twitter Web App   |      364     |                                 @hbthen3rd I know I don't.                                | 1.19558E+18 |
|   45:25.0  |        0        |    []    |          |  16:07.4 | Eric |        None       |    None    |       0       |                        None                        |  narutouz16 |   Twitter Web App   |      364     |        @TonyKelly95 I'm still not satisfied in the ending, even though its longer.        | 1.19558E+18 |
|   37:42.0  |        0        |    []    |          |  16:07.4 | Eric |        None       |    None    |       0       |                        None                        |  narutouz16 | Twitter for Android |      364     |               I'm currently in second place in my leaderboards in duolongo.               | 1.19544E+18 |
|   41:26.0  |        2        |    []    |          |  16:07.4 | Eric |        None       |    None    |       0       |                        None                        |  narutouz16 | Twitter for Android |      364     |                         @TheRealRotimi live footage of us at spin.                        | 1.19515E+18 |
|   40:08.0  |        0        |    []    |          |  16:07.4 | Eric |        None       |    None    |       0       |                        None                        |  narutouz16 | Twitter for Android |      364     | Duolingo has more content than I thought, add that to another one of my monthly payments. | 1.19515E+18 |
|   05:37.0  |        1        |    []    |          |  16:07.4 | Eric |        None       |    None    |       0       |                        None                        |  narutouz16 | Twitter for Android |      364     |                                @TonyKelly95 It dont go down                               |  1.1951E+18 |
|   29:45.0  |        0        |    []    |          |  16:07.4 | Eric |        None       |    None    |       0       |                        None                        |  narutouz16 |   Twitter Web App   |      364     |         This is my meme day, where I explore the internet for memes. your welcome.        | 1.19482E+18 |
|   18:57.0  |        0        |    []    |          |  16:07.4 | Eric |        None       |    None    |     67134     |                        Blah                        |  narutouz16 |   Twitter Web App   |      364     |                                      RT @DitzyFlama:                                      | 1.19482E+18 |

我想创建一个新列is_retweet,该列基本上基于retweet_text列。 如果retweet_text = 'None',则is_retweet = 0 else 1

类似的东西:

df['is_retweet'] = [if 'retweet_text' = 'None' then 0 else 1]

但是它给了我

    df['is_retweet'] = [if 'retweet_text' = 'None' then 0 else 1]
                         ^
SyntaxError: invalid syntax

下面的完整代码:

import pandas as pd

df = pd.read_csv("filepath")

df['is_retweet'] = [if 'retweet_text' = 'None' then 0 else 1]

0 个答案:

没有答案