- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在做一个项目,需要填写“向量”月份(制作直方图:每月推文数量的概述)。为了填写矢量月,我编写了以下代码:
numTweets = list(tweets_cleaned_panda.iloc[:,1])
months = [0, 0, 0, 0, 0, 0, 0]
for i in range(0,len(numTweets)+1):
if tweets_cleaned_panda['created_at'].str.contains("Mar") or tweets_cleaned_panda['created_at'].str.contains("Apr"):
months[0] = months[0] + 1
elif tweets_cleaned_panda['created_at'].str.contains("May"):
months[1] += 1
elif tweets_cleaned_panda['created_at'].str.contains("Jun"):
months[2] += 1
elif tweets_cleaned_panda['created_at'].str.contains("Jul"):
months[3] += 1
elif tweets_cleaned_panda['created_at'].str.contains("Aug"):
months[4] += 1
elif tweets_cleaned_panda['created_at'].str.contains("Sept"):
months[5] += 1
else:
months[6] += 1
print months
我尝试将 .any() 附加到 contains() 语句的末尾,但它只填充月份[0]。
此外,我编写了以下代码:
for i in range(0,len(numTweets)+1):
np.where(tweets_cleaned_panda['created_at'].str.contains("Mar"),
months[0] = months[0] + 1,
np.where(tweets_cleaned_panda['created_at'].str.contains("Apr"),
months[0] = months[0] + 1,
np.where(tweets_cleaned_panda['created_at'].str.contains("May"),
months[1] = months[1] + 1,
np.where(tweets_cleaned_panda['created_at'].str.contains("Jun"),
months[2] = months[2] + 1,
np.where(tweets_cleaned_panda['created_at'].str.contains("Jul"),
months[3] = months[3] + 1,
np.where(tweets_cleaned_panda['created_at'].str.contains("Aug"),
months[4] = months[4] + 1,
np.where(tweets_cleaned_panda['created_at'].str.contains("Sept"),
months[5] = months[5] + 1,
np.where(tweets_cleaned_panda['created_at'].str.contains("Oct"),
months[6] =months[]+ 1))))))))
但这给出了以下错误:
SyntaxError: keyword can't be an expression File "", line 10 months[0] = months[0] + 1, SyntaxError: keyword can't be an expression
有谁可以帮忙吗?
最佳答案
pandas 非常适合处理日期时间数据。使用 pd.to_datetime
函数您可以转换 UTC 格式的时间:
pd.to_datetime("Wed Aug 27 13:08:45 +0000 2008")
Out Timestamp('2008-08-27 13:08:45')
如果您首先使用以下方式转换该列:
df['created_at'] = pd.to_datetime(df['created_at'])
然后您可以对月份列使用 .dt
访问器:
df['month'] = df['created_at'].dt.month
要从中获取频率分布,您所需要做的就是调用 value_counts
:
df['month'].value_counts()
注意:您需要将 df
替换为 DataFrame 的名称 (tweets_cleaned_panda
)。
关于python - 如何在 Pandas 中实现 if/elif/else 语句 (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40823399/
我想压缩一些看起来像这样的湿代码: if slips[i] 0: ac.setBackgroundOpacity(wheel['slip'], 1) 请注意,least_bound_inde
我有 5 组请求的类别定义为 python 字典,例如: category1 = {'type1', 'type2', 'type3'} category2 = {'type4', 'type5'}
这个问题在这里已经有了答案: Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of
我在网站上使用 Django,需要构建一个上下文处理器来提供引荐来源网址(名为 referer 的变量)信息。 我有一个简单的 if、elif、elif、else 语句: [ . . . ] host
这是我的个人项目,现在我的程序是大量 if 语句,这些语句基于我制作的字典,其中包含每一季的剧集编号(不同的季节有不同的剧集数)和标题。我得到了想要的结果,但是我一直在尝试寻找一种更高级的方法,例如使
在类方法中,我有一组针对单个关键字参数的可能选项,每个选项都有不同的算法来计算某些内容。为了检查哪个选项已添加到关键字中,我创建了一系列 if、elif、else 也找到了提供的关键字选项。 clas
所以我试图在 bash 脚本中执行 if else if else 语句。截至目前,当我运行此脚本时,我收到以下错误消息“./groupJobs.sh: line 76: syntax error n
我有一个 in if-elif-elif-else 语句,其中 99% 的时间执行 else 语句: if something == 'this': doThis() elif somethi
如果这是一个非常简单的问题,请原谅我。我已经在这几个小时了。我正在尝试每行读取一个 csv 文件。但是在最后一行,我不希望它附加任何“,”,这就是为什么我在 if-elif 语句中将问题分开。 我不确
税收计算器 def computeTax(maritalStatus,userIncome): if maritalStatus == "Single": print("User is s
我正在编写一个函数来清理字典数据,其中包含一个显示日期和该日期降雨量的键值对。数据清洗的条件是去除满足以下条件的和键值对: 类型不是整数或 float 。即使值是一个可以转换为整数的字符串(例如"5"
我正在编写一个函数来清理字典数据,其中包含一个显示日期和该日期降雨量的键值对。数据清洗的条件是去除满足以下条件的和键值对: 类型不是整数或 float 。即使值是一个可以转换为整数的字符串(例如"5"
这里有一些代码由于不正确的语句而不起作用 #Calculator print("This is a program that can be used as a calculator") print("
我想知道是否有更好的写法。下面所述的当前代码有效。我只是想知道是否有更好的方法。 这是为了验证。因此,if 语句检查所有必填字段,如果它们可以接受,则它会进入执行代码。问题开始是因为 的输入字段 se
我正在尝试使用 if elif 测试一个条件,但我没有得到想要的结果。请帮助我确定我做错了什么? #include using namespace std; #define a(t1)(t1+50)
#!/bin/bash if [ "$1" = "boot" ] then if [ -f /var/log/boot.log ] then echo /var/
./chkf: line 30: syntax error near unexpected token `elif' '/chkf: line 30: `elif [ -f "$object" ] ;
我想提取 Pandas 数据框中格式不一致的数据字段的位置。 (我不维护数据,因此无法更改此字段的格式。) 运行以下玩具版本 string2 = 'Denver.John' if string2.fi
亲们, 您将如何用字典重写“checkme”函数中的 if/elif? def dosomething(queue): ... def checkme(queue): """ Consum
我是 Python 的新手,我用它来做一个丑陋的小 put-this-tabular-data-into-a-db 转换。该程序查看数据,在 MySQL 中创建一个表,然后将数据读入表中。在本节中,将
我是一名优秀的程序员,十分优秀!