【问题标题】:Loop text data based on column value in data frame in python基于python中数据框中的列值循环文本数据
【发布时间】:2021-10-01 05:08:34
【问题描述】:

我有一个名为 data_set_tweets.csv 的数据集,如下所示

created_at,tweet,retweet_count
2021 年 7 月 29 日 2:40,伦敦板球大晴天,3
2021 年 7 月 29 日 10:40,英格兰队击球得分,0
7/29/2021 11:50,英格兰队赢了比赛,1

我试图做的是将以下输出放入数据框中。
这意味着我想根据 retweet_count 值迭代 tweet 列中的文本,并在该特定推文上使用相同的 created_at 值
以下是我的数据集的预期输出

created_at,tweet
2021 年 7 月 29 日 2:40,伦敦板球比赛的晴天
2021 年 7 月 29 日 2:40,伦敦板球大晴天
2021 年 7 月 29 日 2:40,伦敦板球大晴天
2021 年 7 月 29 日 2:40,伦敦板球大晴天
2021 年 7 月 29 日 10 点 40 分,英格兰队击球取得好成绩
7/29/2021 11:50,英格兰队赢得比赛
7/29/2021 11:50,英格兰队赢了比赛


以下是我如何开始我的方法
import pandas as pd

def iterateTweets():
tweets = pd.read_csv(r'data_set_tweets.csv')
df = pd.DataFrame(tweets, columns=['created_at', 'tweet', 'retweet_count'])
df['created_at'] = pd.to_datetime(df['created_at'])
df['tweet'] = df['tweet'].apply(lambda x: str(x))
df['retweet_count'] = df['retweet_count'].apply(lambda x: str(x))

# print(df)
return df

if __name__ == '__main__':

print(iterateTweets())

我是数据框和python的初学者,有人可以帮助我吗?

【问题讨论】:

    标签: python pandas dataframe nlp


    【解决方案1】:

    使用Index.repeatDataFrame.loc 用于重复列,DataFrame.pop 用于使用和删除列:

    df = pd.read_csv(r'data_set_tweets.csv')
    
    df['created_at'] = pd.to_datetime(df['created_at'])
    df = df.loc[df.index.repeat(df.pop('retweet_count') + 1)].reset_index(drop=True)
    print (df)
               created_at                                  tweet
    0 2021-07-29 02:40:00  Great Sunny day for Cricket at London
    1 2021-07-29 02:40:00  Great Sunny day for Cricket at London
    2 2021-07-29 02:40:00  Great Sunny day for Cricket at London
    3 2021-07-29 02:40:00  Great Sunny day for Cricket at London
    4 2021-07-29 10:40:00  Great Score put on by England batting
    5 2021-07-29 11:50:00                  England won the match
    6 2021-07-29 11:50:00                  England won the match
    

    【讨论】:

      【解决方案2】:

      或使用:

      df = df.apply(lambda x: x.repeat(df['retweet_count'] + 1)).reset_index(drop=True)
      

      如果要删除retweet_count 列:

      df = df.apply(lambda x: x.repeat(df['retweet_count'] + 1)).reset_index(drop=True).drop('retweet_count', axis=1)
      

      或者:

      col = df.pop('retweet_count') + 1
      df = df.apply(lambda x: x.repeat(col)).reset_index(drop=True)
      

      df 输出:

                 created_at                                  tweet
      0 2021-07-29 02:40:00  Great Sunny day for Cricket at London
      1 2021-07-29 02:40:00  Great Sunny day for Cricket at London
      2 2021-07-29 02:40:00  Great Sunny day for Cricket at London
      3 2021-07-29 02:40:00  Great Sunny day for Cricket at London
      4 2021-07-29 10:40:00  Great Score put on by England batting
      5 2021-07-29 11:50:00                  England won the match
      6 2021-07-29 11:50:00                  England won the match
      

      或将locenumerate 一起使用:

      df.loc[sum([[i] * (v + 1) for i, v in enumerate(df['retweet_count'])], [])].reset_index(drop=True)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-04-08
        • 2020-09-11
        • 2019-03-31
        • 1970-01-01
        • 2019-06-05
        • 1970-01-01
        • 2022-11-18
        相关资源
        最近更新 更多