社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python pandas dataframe:如何分组来自不同列的值

Rachele Povelato • 5 年前 • 1607 次点击  

我需要帮助清理数据框。 数据帧如下:

         Gap      Date          Time      Full text   Retweets   Likes
0   3.160003  2018-05-21    03:30:56  @georgechang..  19         462
1   3.160003  2018-05-21    21:15:03  @reveal         141        1610
2   3.160003  2018-05-21    11:25:21  RT @nova_road:  2030       0
3   3.160003  2018-05-21    07:10:01  @MrsYomaddy     48         917
4   3.160003  2018-05-21    07:06:54  @Dani21 @dmatki 40         5367

可以看到,对于所有行,间隙值都等于日期值。

我希望获得以下数据帧:

                         num    Time      Full text    Retweets   Likes
    Gap       Date         
0   3.160003  2018-05-21    1     03:30:56  .....        19      462
1                           2     21:15:03  .....        141     1610
2                           3     11:25:21  .....        2030    0 
3                           4     07:10:01  .....        48      917
4                           5     07:06:54  .....        40      5367

其中num是带有tweets数量的额外列。

我已经问了一个类似的问题,但现在问题有点不同了。 这是链接。 How can I create a multiindex data frame with the following datasets? 多索引数据帧

我试图做的是以下代码:

StockbyTweets.set_index(['Date','Gap','Time'],inplace=True)
StockbyTweets

但我得到的只是:

                           Time       Full text    Retweets   Likes
    Gap       Date         
0   3.160003  2018-05-21    03:30:56  .....        19        462
1                           21:15:03  .....        141       1610
2                           11:25:21  .....        2030      0 
3                           07:10:01  .....        48        917
4                           07:06:54  .....        40        5367

我如何获得一个额外的列与tweet的数量?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/47973
 
1607 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Sid
Reply   •   1 楼
Sid    6 年前

set_index 你要找的是: Documents

df.set_index(['Gap','Date'])

没有注意到问题的另一部分。

以下是tweets栏的数量:

level_name = df.index.get_level_values(0).tolist()
level_name = [str(i).split(' ')[0] for i in level_name]
level_name = list(set(level_name))

num_of_tweets = {}
for i in level_name:
    df1 = df.loc[i]
    num_of_tweets[i] = len(df1)

df.reset_index(inplace=True)
df['num_of_tweets'] = 0
for key in num_of_tweets.keys():

    df.loc[df['Gap'] == key,'num_of_tweets'] = num_of_tweets[key]

# set the index again.

逻辑有点迂回,可能不是最好的解决办法。

但是,逻辑可用于获取列的任意组合。