小编hel*_*vel的帖子

Python:如何在多系列数据帧中找到第一个非零值?

我有一个数据框,其中有5列已编入索引.数据由0和1组成.我想在每个系列中找到第一个非零并且将它(并且只有它)乘以100.

Date A B C D E 3/1/16 0 0 0 0 0 3/2/16 0 0 1 0 0 3/3/16 1 0 0 0 0 3/4/16 0 1 0 0 0 3/7/16 0 0 1 0 1 3/8/16 0 0 0 0 1 3/9/16 0 0 0 1 1 我尝试了以下代码,但它没有用.

for col in df.columns:
    idx = df[col].first_valid_index()
    df[col][idx] = df[col][idx]*100.
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

2
推荐指数
1
解决办法
48
查看次数

错误:需要类似字节的对象,而不是“str”

我正在玩一些我在网上找到的代码。它在 Python 2 中。当我在 Python 3 中运行代码时,它给了我这个错误:需要一个类似字节的对象,而不是“str”。有人可以帮我解决这个问题吗?非常感谢您

import urllib.request as ur 
text = 
 ur.urlopen('https://raw.githubusercontent.com/ryanmcdermott/trump-
speeches/master/speeches.txt')  
words = []  
for line in text:  
    line = line.decode('utf-8-sig', errors='ignore')
    line = line.encode('ascii', errors='ignore')
    line = line.replace('\r', ' ').replace('\n', ' ')
    new_words = line.split(' ')
    new_words = [word for word in new_words if word not in ['', ' ']]
    words = words + new_words

print('Corpus size: {0} words.'.format(len(words)))  
Run Code Online (Sandbox Code Playgroud)

python

2
推荐指数
1
解决办法
4995
查看次数

基本Python:如何规范化数据系列?

我有一个数据框,其中有5列按日期索引.我想通过列表中的第一项来规范化这些数据系列.

                A   B   C   D   E
    1/1/2017    3   4   1   2   3
    1/2/2017    7   4   4   3   3
    1/3/2017    2   5   5   4   3
    1/4/2017    2   5   3   6   3
    1/5/2017    2   2   2   6   6
Run Code Online (Sandbox Code Playgroud)

例如,在A栏中,我想将所有内容除以3,即列表中的第一项.B列到E列相同.

谢谢你的帮助!

python pandas

1
推荐指数
1
解决办法
46
查看次数

标签 统计

python ×3

pandas ×2

dataframe ×1