我有以下数据帧:
import pandas as pd
import numpy as np
df = pd.DataFrame(dict(A = np.arange(3),
B = np.random.randn(3),
C = ['foo','bar','bah'],
D = pd.Timestamp('20130101')))
print(df)
A B C D
0 0 -1.087180 foo 2013-01-01
1 1 -1.343424 bar 2013-01-01
2 2 -0.193371 bah 2013-01-01
Run Code Online (Sandbox Code Playgroud)
dtypes 对于列:
print(df.dtypes)
A int32
B float64
C object
D datetime64[ns]
dtype: object
Run Code Online (Sandbox Code Playgroud)
但在使用apply它们之后所有对象的更改:
print(df.apply(lambda x: x.dtype))
A object
B object
C object
D object
dtype: object
Run Code Online (Sandbox Code Playgroud)
为什么dtypes强迫反对?我认为apply只应在列中考虑.
pandas 0.17.1
python …
如何添加一个装饰器,声明函数传入的 pandas 数据帧参数具有日期时间索引?
我查看了 engarde 和 validada 包,但还没有找到任何东西。我可以在函数内部进行此检查,但更喜欢装饰器。
我有一个熊猫系列 df(日期 = 索引):
2015-09-10 58
2015-09-11 40
2015-09-12 33
2015-09-13 42
2015-09-14 22
2015-09-15 88
2015-09-16 99
2015-09-17 124
Run Code Online (Sandbox Code Playgroud)
我想将日期从 2015-09-11 删除到 2015-09-15,所以我的 df 看起来像:
2015-09-10 58
2015-09-16 99
2015-09-17 124
Run Code Online (Sandbox Code Playgroud)
我试过使用 df.drop["2015-09-11":"2015-09-15"],但出现错误:
TypeError: 'instancemethod' object has no attribute '__getitem__'
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?
谢谢!
如果简化这个,最好的方法是什么?
#!/usr/bin/python
ans=input("choose yes or no: ")
if ans == "yes" or ans == "YES" or ans == "y" or ans == "Y":
print("ok")
else:
print("no")
Run Code Online (Sandbox Code Playgroud) 假设我有两个由另一个函数生成的列表:
test = [[0, 1], [0, 2], [1, 5], [1,6], [2, 0], [3, 99], [3, 89], [3, 79]]
test2 = [[1, 4], [4, 1]]
Run Code Online (Sandbox Code Playgroud)
我想将它们转换为关联数组,以便快速查找,如下所示:
test: {0: [1, 2], 1: [5,6], 2: [0], 3: [99, 98, 97]}
test2: {1: [4], 4: [1]}
Run Code Online (Sandbox Code Playgroud)
我可以这样做:
def list_to_dict(my_list):
last_val = my_list[0][0]
temp = []
my_dict = {}
for i in my_list:
if last_val == i[0]:
temp.append(i[1])
else:
#add the values to this key
my_dict[last_val] = temp
#reset the list
temp = []
temp.append(i[1]) …Run Code Online (Sandbox Code Playgroud) 对于数据框
import pandas as pd
df=pd.DataFrame({'group':list("AADABCBCCCD"),'Values':[1,0,1,0,1,0,0,1,0,1,0]})
Run Code Online (Sandbox Code Playgroud)
我试图绘制一个显示时间百分比A, B, C, D为零(或一个)的条形图。
我有一个可行的方法,但我认为必须有更直接的方法
tempdf=df.groupby(['group','Values']).Values.count().unstack().fillna(0)
tempdf['total']=df['group'].value_counts()
tempdf['percent']=tempdf[0]/tempdf['total']*100
tempdf.reset_index(inplace=True)
print tempdf
sns.barplot(x='group',y='percent',data=tempdf)
Run Code Online (Sandbox Code Playgroud)
如果仅绘制平均值,我可以sns.barplot在df数据框上比tempdf 做更多。如果我对绘制百分比感兴趣,我不确定如何优雅地做到这一点。
谢谢,
我有一个DataFrame:
Actual Pred
Date
2005-04-01 10.2 10.364470
2005-05-01 9.4 9.542778
2005-06-01 9.5 9.684794
2005-07-01 9.4 9.547604
2005-08-01 9.7 9.768893
Run Code Online (Sandbox Code Playgroud)
我想为每个DataFrame的索引添加一个月,所以它看起来像这样:
Actual Pred
Date
2005-05-01 10.2 10.364470
2005-06-01 9.4 9.542778
2005-07-01 9.5 9.684794
2005-08-01 9.4 9.547604
2005-09-01 9.7 9.768893
Run Code Online (Sandbox Code Playgroud)
我怎么做?
重要评论:
当我命令print type(DataFrame.index[0])找出索引的数据类型时,我得到:
<class 'pandas.tslib.Timestamp'>
Run Code Online (Sandbox Code Playgroud)
只是为了让你知道这是一个熊猫时间戳.
我使用以下代码.我的所有CSV文件都具有统一的结构.形成数据框时,它在我的CSV中包含两列日期.
在结果数据框中,对于少数行,日期值在第一个日期列中,而对于其余数据,它将转到第二个日期列.
不知道为什么要为源CSV文件中的一列生成两列(日期列).
all_data = pd.DataFrame()
for f in glob.glob("/Users/tcssig/Desktop/Files/*.csv"):
df = pd.read_csv(f)
all_data = all_data.append(df,ignore_index=True)
In [76]: all_data.columns
Out[76]: Index(['0', '0.1', 'Channel_ID', 'Date', 'Date ', 'Duration (HH:MM)','Episode #', 'Image', 'Language', 'Master House ID', 'Parental Rating','Program Category', 'Program Title', 'StartTime_ET', 'StartTime_ET2','Synopsis'],
dtype='object')
Run Code Online (Sandbox Code Playgroud) 我有大约 25 个具有相同列标题的数据框,我需要将它们相互附加。我过去曾使用 24 次 .append() 调用尝试过此操作,但没有成功。有没有一种简单的方法可以做到这一点?
我有一个这样的列表:
c = ['A','B','C']
Run Code Online (Sandbox Code Playgroud)
我希望它将它转换为dict像对象一样
d = {"alphabets":{"0":"A","1":"B","2":"C"}}
Run Code Online (Sandbox Code Playgroud)
我目前已实现以下代码:
c = ["A","B","C"]
d={}
for i in range(len(c)):
d.update({"alphabets":{str(i): c[i]}})
print d
Run Code Online (Sandbox Code Playgroud)
但这给出了输出{'alphabets': {'2': 'C'}}.有谁知道如何处理这个bug?