除了列表中的单词,我如何命名所有单词,保留?
keep = ['for', 'any', 'a', 'vs']
df.col
``
0 1. The start for one
1 2. Today's world any
2 3. Today's world vs. yesterday.
Run Code Online (Sandbox Code Playgroud)
预期输出:
number title
0 1 The Start for One
1 2 Today's World any
2 3 Today's World vs. Yesterday.
Run Code Online (Sandbox Code Playgroud)
我试过
df['col'] = df.col.str.title().mask(~clean['col'].isin(keep))
Run Code Online (Sandbox Code Playgroud) 如何快速删除 A、B 和 C 均为假的行?我试过:
df3 = df[~(~(df['A'])& ~(df['B']) & ~(df['C']) )]
df3
com A B C
0 as TRUE FALSE FALSE
1 da TRUE FALSE FALSE
Run Code Online (Sandbox Code Playgroud) 如何使用迭代类型的规则创建 col2?
col2 = col1 + 0.1(col2's previous value). If there is no previous value (here in 20 Dec 2019), then col2 should equal col1
df
date col1
2019-12-20 10
2019-12-27 3
2020-01-03 7
Run Code Online (Sandbox Code Playgroud)
预期产出
date col1 col2
2019-12-20 10 10 (no previous value, so equal col1)
2019-12-27 3 4 (3+0.1*10)
2020-01-03 7 7.4 (7+0.1*4)
Run Code Online (Sandbox Code Playgroud) df
Date Value CODE
0 2020-03-12 7 AFG
1 2020-03-11 7 AFG
...
Run Code Online (Sandbox Code Playgroud)
我如何从代码生成国家/地区?我尝试从 CODE 变量生成代码
import country_converter as coco
df['country'] = df['CODE'].apply(coco.convert)
Run Code Online (Sandbox Code Playgroud)
Actual Output:
Date Value CODE country
0 2020-03-12 7 AFG AFG
1 2020-03-11 7 AFG AFG
Run Code Online (Sandbox Code Playgroud)
Expected Output:
Date Value CODE country
0 2020-03-12 7 AFG Afghanistan
1 2020-03-11 7 AFG Afghanistan
Run Code Online (Sandbox Code Playgroud) 如何从 AF 列及以后读取 Excel 文件?我不知道最后一列字母名称,而且文件太大,无法不断检查。
df = pd.read_excel(r"Documents\file.xlsx", usecols="AF:")
Run Code Online (Sandbox Code Playgroud)