社区所有版块导航
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
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Erfan

Erfan 最近创建的主题
Erfan 最近回复了
6 年前
回复了 Erfan 创建的主题 » 在Pandas Python中基于列文本创建列

使用 np.select 要创建多条件列:

letter = df['Name'].str[0]
df['Company'] = np.select([letter.eq('A'), letter.eq('B')], ['Alpha', 'Bravo'], default='Other')

    Name Company
0  A1001   Alpha
1  A1002   Alpha
2  B1001   Bravo
3  C1001   Other
4  A1003   Alpha
5  B1002   Bravo
6  B1003   Bravo
7  C1002   Other
8  D1001   Other
9  D1002   Other

同样的方法,但现在我们使用一个更“自我解释”的代码:

letter = df['Name'].str[0]
conditions = [letter.eq('A'), letter.eq('B')]
choices = ['Alpha', 'Bravo']

df['Company'] = np.select(conditions, choices, default='Other')
7 年前
回复了 Erfan 创建的主题 » 如何将Python中的日期时间列重置为1分钟

首先我们转换你的 State Time 列到 datetime 类型。然后我们使用 pd.date_range 并使用 first 以频率为起点的时间 1 minute .

df['State Time'] = pd.to_datetime(df['State Time'])
df['State Time'] = pd.date_range(start=df['State Time'].min(), 
                                 periods=len(df), 
                                 freq='min').time

产量

     ID State Time  End Time
0  A001   12:00:00  12:00:00
1  A002   12:01:00  12:00:00
2  A003   12:02:00  12:00:00
3  A004   12:03:00  12:00:00
4  A005   12:04:00  12:00:00
5  A006   12:05:00  12:00:00
6  A007   12:06:00  12:00:00
7 年前
回复了 Erfan 创建的主题 » python-从pandas系列中删除stopwords的函数

如果你真的想 define 你自己可以使用的功能 .apply 在那之后:

from nltk.corpus import stopwords

df = pd.DataFrame(index=['the', 'American', 'a', 'I', 'hilarious', 'Mexican', 'is'],
                  data={ 0:[3425, 431, 213, 124, 53, 23, 2]})

# Clean up dataframe and convert words to lowercase
df['words'] = df.index.str.lower()
df.reset_index(drop=True, inplace=True)

# Define our function to remove stopwords
def remove_stopwords(word):
    if word not in stopwords.words('english'):
        return word
    else:
        return ''

# Apply the function to our words column to clean up.
df['words_clean'] = df.words.apply(remove_stopwords)
print(df)
      0      words words_clean
0  3425        the            
1   431   american    american
2   213          a            
3   124          i            
4    53  hilarious   hilarious
5    23    mexican     mexican
6     2         is             
7 年前
回复了 Erfan 创建的主题 » 如何使用pandas对python中的行进行多列排序

我们可以用 string.ascii_lowercase 使每一列 rank 结束 axis=1

import string

cols = ['Revenue', 'SaleCount', 'salesprices']

for index, col in enumerate(cols):
    df[f'rank{string.ascii_lowercase[index]}'] = df[cols].rank(axis=1)[col]

输出:

print(df)
   Revenue        Date  SaleCount  salesprices  ranka  rankb  rankc
0      300  2016-12-02         10         8000    2.0    1.0    3.0
1     9000  2016-12-02        100         1000    3.0    1.0    2.0
2     1000  2016-12-02         30          500    3.0    1.0    2.0
3      750  2016-12-02         35          700    3.0    1.0    2.0
4      500  2016-12-02         20         2500    2.0    1.0    3.0
5     2000  2016-12-02        100         3800    2.0    1.0    3.0
6        0  2016-12-02          0           16    1.5    1.5    3.0
7      600  2016-12-02         30         7400    2.0    1.0    3.0
8       50  2016-12-02          2         3200    2.0    1.0    3.0
9      500  2016-12-02         20           21    3.0    1.0    2.0

音符 我用过 f-string 它仅在python版本>3.4中受支持。 否则使用 .format 字符串格式如下:

import string

cols = ['Revenue', 'SaleCount', 'salesprices']

for index, col in enumerate(cols):
    df['rank{}'.format(string.ascii_lowercase[index])] = df[cols].rank(axis=1)[col] 
7 年前
回复了 Erfan 创建的主题 » python pandas str.从多个列提取

你快到了,你可以做以下事情。 我们可以用 for loop 申请 str.extract 两次创建两个临时列。

之后创建最后一列 result 具有 fillna 。

cols = ['field1', 'field2']
n=1
for col in cols:
    df['result'+str(n)] = df[col].str.extract('([0-9]{4})')
    n += 1

df['result'] = df.result1.fillna(df.result2).fillna('')
df.drop(['result1', 'result2'], inplace=True, axis=1)

print(df)
   field1  field2 result
0  ab1234  ab1234   1234
1  ac1234           1234
2    qw45    rt23       
3  c1234b  cb1234   1234
4      cv  1234dd   1234