Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果elif语句不创建新的数据帧列_Python_Pandas - Fatal编程技术网

Python 如果elif语句不创建新的数据帧列

Python 如果elif语句不创建新的数据帧列,python,pandas,Python,Pandas,嗨,我想在if-elif语句中创建一些新的数据帧列 但是,当我运行代码时,if-elif语句不会创建任何列 当我将代码置于if-elif语句之外时,pandas使用我正在使用的语法创建列 我需要做什么才能让pandas在if elif语句中创建新列 data = '3 months' if data == '12 months': df['calc_year'] = df['quarter'] * 4 elif data == '3 months': df['calc_quar

嗨,我想在if-elif语句中创建一些新的数据帧列

但是,当我运行代码时,if-elif语句不会创建任何列

当我将代码置于if-elif语句之外时,pandas使用我正在使用的语法创建列

我需要做什么才能让pandas在if elif语句中创建新列

data = '3 months'

if data == '12 months':
    df['calc_year'] = df['quarter'] * 4
elif data == '3 months':
    df['calc_quarter'] = df['year'] / 4
我测试了if-elif语句,它是有效的,只是不创建列

data = 30

if data > 20:
    message = "yep"
elif data <= 20:
    message = "nope"
print(message)
data=30
如果数据>20:
message=“是”

elif data你的代码对我有用

我刚刚用随机整数生成了
季度
年度
列,并使用了您的条件,它给出了正确的输出

import pandas as pd
import numpy as np

df = pd.DataFrame()
df['quarter'] = np.random.randint(1, 6, 10)
df['year'] = np.random.randint(1, 6, 10)


data = '3 months'

if data == '12 months':
    df['calc_year'] = df['quarter'] * 4
elif data == '3 months':
    df['calc_quarter'] = df['year'] / 4
df


# output
df
Out[8]: 
   quarter  year  calc_quarter
0        1     5          1.25
1        3     2          0.50
2        4     5          1.25
3        1     5          1.25
4        1     3          0.75
5        5     4          1.00
6        1     2          0.50
7        4     4          1.00
8        4     4          1.00
9        1     3          0.75

你的代码对我有用

我刚刚用随机整数生成了
季度
年度
列,并使用了您的条件,它给出了正确的输出

import pandas as pd
import numpy as np

df = pd.DataFrame()
df['quarter'] = np.random.randint(1, 6, 10)
df['year'] = np.random.randint(1, 6, 10)


data = '3 months'

if data == '12 months':
    df['calc_year'] = df['quarter'] * 4
elif data == '3 months':
    df['calc_quarter'] = df['year'] / 4
df


# output
df
Out[8]: 
   quarter  year  calc_quarter
0        1     5          1.25
1        3     2          0.50
2        4     5          1.25
3        1     5          1.25
4        1     3          0.75
5        5     4          1.00
6        1     2          0.50
7        4     4          1.00
8        4     4          1.00
9        1     3          0.75

使用df.assign而不是assign,因为您的问题意味着不存在列年

替换

if data == '12 months':
    df['calc_year'] = df['quarter'] * 4
elif data == '3 months':
    df['calc_quarter'] = df['year'] / 4


使用df.assign而不是assign,因为您的问题意味着不存在列年

替换

if data == '12 months':
    df['calc_year'] = df['quarter'] * 4
elif data == '3 months':
    df['calc_quarter'] = df['year'] / 4


共享您的示例数据帧…如果您有一个带有“年”和“季度”列的df,那么您的代码应该编译。当您的代码通过if-elif语句运行时,df将被修改。代码正常工作。您被困在哪里了?共享您的示例数据帧…如果您有一个包含“年”和“季度”列的df,那么您的代码应该可以编译。当您的代码通过if-elif语句运行时,df将被修改。代码正常工作。你被困在哪里?