我有一个Python Pandas数据帧df:
d=[['hello',1,'GOOD','long.kw'],
[1.2,'chipotle',np.nan,'bingo'],
['various',np.nan,3000,123.456]]
t=pd.DataFrame(data=d, columns=['A','B','C','D'])
Run Code Online (Sandbox Code Playgroud)
看起来像这样:
print(t)
A B C D
0 hello 1 GOOD long.kw
1 1.2 chipotle NaN bingo
2 various NaN 3000 123.456
Run Code Online (Sandbox Code Playgroud)
我想创建一个新的列是一个list
中值的A
,B
,C
,和D
.所以它看起来像这样:
t['combined']
Out[125]:
0 [hello, 1, GOOD, long.kw]
1 [1.2, chipotle, nan, bingo]
2 [various, nan, 3000, 123.456]
Name: combined, dtype: object
Run Code Online (Sandbox Code Playgroud)
我正在尝试这段代码:
t['combined'] = t.apply(lambda x: list([x['A'],
x['B'],
x['C'],
x['D']]),axis=1)
Run Code Online (Sandbox Code Playgroud)
哪个返回此错误:
ValueError: Wrong number of items passed 4, placement implies 1 …
Run Code Online (Sandbox Code Playgroud) 我有以下数据帧df:
data={'id':[1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2],
'value':[2,2,3,2,2,2,3,3,3,3,1,4,1,1,1,4,4,1,1,1,1,1]}
df=pd.DataFrame.from_dict(data)
df
Out[8]:
id value
0 1 2
1 1 2
2 1 3
3 1 2
4 1 2
5 1 2
6 1 3
7 1 3
8 1 3
9 1 3
10 2 1
11 2 4
12 2 1
13 2 1
14 2 1
15 2 4
16 2 4
17 2 1
18 2 1
19 2 1
20 2 1
21 2 1
Run Code Online (Sandbox Code Playgroud)
我需要做的是在id级别(df.groupby ['id'])识别,当值连续显示相同的数字达3次或更多次时.
我希望以上结果如下:
df
Out[12]:
id …
Run Code Online (Sandbox Code Playgroud) 我有一个带有零星日期的数据框作为索引,列='id'和'num'.我想要pd.groupby
'id'列,并将reindex应用于数据帧中的每个组.
我的示例数据集如下所示:
id num
2015-08-01 1 3
2015-08-05 1 5
2015-08-06 1 4
2015-07-31 2 1
2015-08-03 2 2
2015-08-06 2 3
Run Code Online (Sandbox Code Playgroud)
我的预期输出一次pd.reindex
用ffill
的是:
id num
2015-08-01 1 3
2015-08-02 1 3
2015-08-03 1 3
2015-08-04 1 3
2015-08-05 1 5
2015-08-06 1 4
2015-07-31 2 1
2015-08-01 2 1
2015-08-02 2 1
2015-08-03 2 2
2015-08-04 2 2
2015-08-05 2 2
2015-08-06 2 3
Run Code Online (Sandbox Code Playgroud)
我试过这个,除其他外无济于事:
newdf=df.groupby('id').reindex(method='ffill')
哪个返回错误:AttributeError: Cannot access callable attribute 'reindex' of …
我想使用一个名为 Pandas 的数据框df
,它有一个 ID 列和一个包含可变数量元组的列表的列表列,所有的元组都具有相同的长度。看起来像这样:
ID list
1 [(0,1,2,3),(1,2,3,4),(2,3,4,NaN)]
2 [(Nan,1,2,3),(9,2,3,4)]
3 [(Nan,1,2,3),(9,2,3,4),(A,b,9,c),($,*,k,0)]
Run Code Online (Sandbox Code Playgroud)
我想将每个列表解包到列 'A','B','C','D' 中,代表每个元组中的固定位置。
结果应如下所示:
ID A B C D
1 0 1 2 3
1 1 2 3 4
1 2 3 4 NaN
2 NaN 1 2 3
2 9 2 3 4
3 NaN 1 2 3
3 9 2 3 4
3 A b 9 c
3 $ * k 0
Run Code Online (Sandbox Code Playgroud)
我尝试过df.apply(pd.Series(list)
但失败了len
,因为不同行上的列表元素不同。需要以某种方式解压到列并按 ID 转置?
我有一个包含一列列表的数据框,可以使用以下命令创建:
import pandas as pd
lists={1:[[1,2,12,6,'ABC']],2:[[1000,4,'z','a']]}
#create test dataframe
df=pd.DataFrame.from_dict(lists,orient='index')
df=df.rename(columns={0:'lists'})
Run Code Online (Sandbox Code Playgroud)
数据框df
如下所示:
lists
1 [1, 2, 12, 6, ABC]
2 [1000, 4, z, a]
Run Code Online (Sandbox Code Playgroud)
我需要创建一个名为' liststring
' 的新列,它将每个列表的每个元素都包含在内,lists
并创建一个字符串,每个元素用逗号分隔.每个列表的元件可以是int
,float
,或string
.结果将是:
lists liststring
1 [1, 2, 12, 6, ABC] 1,2,12,6,ABC
2 [1000, 4, z, a] 1000,4,z,a
Run Code Online (Sandbox Code Playgroud)
我尝试了各种各样的东西,包括从将熊猫DF列表转换为字符串:
df['liststring']=df.lists.apply(lambda x: ', '.join(str(x)))
Run Code Online (Sandbox Code Playgroud)
但不幸的是,结果需要每个字符并用逗号分隔:
lists liststring
1 [1, 2, 12, 6, ABC] [, 1, ,, , 2, ,, , 1, 2, ,, , …
Run Code Online (Sandbox Code Playgroud) 我有一个数据框df
:
data = {'id':[12,112],
'idlist':[[1,5,7,12,112],[5,7,12,111,113]]
}
df=pd.DataFrame.from_dict(data)
Run Code Online (Sandbox Code Playgroud)
看起来像这样:
id idlist
0 12 [1, 5, 7, 12, 112]
1 112 [5, 7, 12, 111, 113]
Run Code Online (Sandbox Code Playgroud)
我需要检查是否id
在中idlist
,然后选择或标记它。我尝试了以下变化并收到注释的错误:
df=df.loc[df.id.isin(df.idlist),:] #TypeError: unhashable type: 'list'
df['flag']=df.where(df.idlist.isin(df.idlist),1,0) #TypeError: unhashable type: 'list'
Run Code Online (Sandbox Code Playgroud)
一些可能的其他解决方法将.apply
在列表理解中?
我在这里寻找一个解决方案,要么选择其中的行id
为idlist
,或标志为1,其中排id
在idlist
。结果df
应为:
id idlist
0 12 [1, 5, 7, 12, 112]
Run Code Online (Sandbox Code Playgroud)
要么:
flag id idlist
0 1 12 [1, 5, 7, 12, 112]
1 0 112 [5, …
Run Code Online (Sandbox Code Playgroud) 我有一个简单的数据框 df 与一列列表lists
。我想基于lists
.
的df
样子:
import pandas as pd
lists={1:[[1]],2:[[1,2,3]],3:[[2,9,7,9]],4:[[2,7,3,5]]}
#create test dataframe
df=pd.DataFrame.from_dict(lists,orient='index')
df=df.rename(columns={0:'lists'})
df
lists
1 [1]
2 [1, 2, 3]
3 [2, 9, 7, 9]
4 [2, 7, 3, 5]
Run Code Online (Sandbox Code Playgroud)
我想df
看起来像这样:
df
Out[9]:
lists rolllists
1 [1] [1]
2 [1, 2, 3] [1, 1, 2, 3]
3 [2, 9, 7, 9] [1, 2, 3, 2, 9, 7, 9]
4 [2, 7, 3, 5] [2, 9, 7, 9, 2, 7, 3, …
Run Code Online (Sandbox Code Playgroud) 我有一个数据框,您可以用它来构建:
dflist=[['123',['abc','qw3','123']],
['ab12',['3e4r5','12we3','asd23','q2w3']]]
df=pd.DataFrame(dflist,columns=['check','checklist'])
Run Code Online (Sandbox Code Playgroud)
看起来像这样:
check checklist
0 123 [abc, qw3, 123]
1 ab12 [3e4r5, 12we3, asd23, q2w3]
Run Code Online (Sandbox Code Playgroud)
我要检查“检查”列中的项目是否在“检查列表”列中的列表中。所以我希望结果数据框看起来像:
check checklist checkisin
0 123 [abc, qw3, 123] True
1 ab12 [3e4r5, 12we3, asd23, q2w3] False
Run Code Online (Sandbox Code Playgroud)
我已经尝试了多种方法,包括以各种形式使用.isin,包括apply / lambda。和直接。
这个:
df['checkisin']=df.check.isin(df.checklist)
Run Code Online (Sandbox Code Playgroud)
产生:
check checklist checkisin
0 123 [abc, qw3, 123] False
1 ab12 [3e4r5, 12we3, asd23, q2w3] False
Run Code Online (Sandbox Code Playgroud)
有两个错误。
尝试这样做:df ['checkisin'] = df.apply(lambda x:x.check.isin(x.checklist))给出此错误:
AttributeError: ("'Series' object has no attribute 'check'", 'occurred at index check')
Run Code Online (Sandbox Code Playgroud)
试试这个:
df['checkisin']=df.apply(lambda x:x['check'] in x.checklist)
Run Code Online (Sandbox Code Playgroud)
给出此错误: …
我有一个数据框“df”,如下所示:
id date1 date2
1 11/1/2016 11/1/2016
1 11/1/2016 11/2/2016
1 11/1/2016 11/1/2016
1 11/1/2016 11/2/2016
1 11/2/2016 11/2/2016
2 11/1/2016 11/1/2016
2 11/1/2016 11/2/2016
2 11/1/2016 11/1/2016
2 11/2/2016 11/2/2016
2 11/2/2016 11/2/2016
Run Code Online (Sandbox Code Playgroud)
我想要做的是按 id 分组,然后获取每个 id 的大小,其中 date1=date2。结果应如下所示:
id samedate count
1 11/1/2016 2
1 11/2/2016 1
2 11/1/2016 2
2 11/2/2016 2
Run Code Online (Sandbox Code Playgroud)
我试过这个:
gb=df.groupby(id').apply(lambda x: x[x.date1== x.date2]['date1'].size())
Run Code Online (Sandbox Code Playgroud)
并得到这个错误:
TypeError: 'int' object is not callable
Run Code Online (Sandbox Code Playgroud)
您当然可以标记 date1 和 date2 相等的每个实例,然后按每个相同日期为每个 id 计算这些标志,但我必须相信有一个 groupby 选项。
我有一个df
可以用这个创建的数据框:
data={'id':[1,1,1,1,2,2,2,2],
'date1':[datetime.date(2016,1,1),datetime.date(2016,1,2),datetime.date(2016,1,3),datetime.date(2016,1,4),
datetime.date(2016,1,2),datetime.date(2016,1,4),datetime.date(2016,1,3),datetime.date(2016,1,1)],
'date2':[datetime.date(2016,1,5),datetime.date(2016,1,3),datetime.date(2016,1,5),datetime.date(2016,1,5),
datetime.date(2016,1,4),datetime.date(2016,1,5),datetime.date(2016,1,4),datetime.date(2016,1,1)],
'score1':[5,7,3,2,9,3,8,3],
'score2':[1,3,0,5,2,20,7,7]}
df=pd.DataFrame.from_dict(data)
And looks like this:
id date1 date2 score1 score2
0 1 2016-01-01 2016-01-05 5 1
1 1 2016-01-02 2016-01-03 7 3
2 1 2016-01-03 2016-01-05 3 0
3 1 2016-01-04 2016-01-05 2 5
4 2 2016-01-02 2016-01-04 9 2
5 2 2016-01-04 2016-01-05 3 20
6 2 2016-01-03 2016-01-04 8 7
7 2 2016-01-01 2016-01-01 3 7
Run Code Online (Sandbox Code Playgroud)
我需要做的就是为每一个的列score1
和score2
,创造它的SUM值两列score1
,并score2
分别根据有无usedate
之间date1 …
我有以下测试DataFrame:
import random
from datetime import timedelta
import pandas as pd
import datetime
#create test range of dates
rng=pd.date_range(datetime.date(2015,1,1),datetime.date(2015,7,31))
rnglist=rng.tolist()
testpts = range(100,121)
#create test dataframe
d={'jid':[i for i in range(100,121)], 'cid':[random.randint(1,2) for _ in testpts],
'stdt':[rnglist[random.randint(0,len(rng))] for _ in testpts]}
df=pd.DataFrame(d)
df['enddt'] = df['stdt']+timedelta(days=random.randint(2,32))
Run Code Online (Sandbox Code Playgroud)
它给出了如下所示的数据框,公司ID列为"cid",唯一的id列为"jid",开始日期为"stdt",enddt为"enddt".
cid jid stdt enddt
0 1 100 2015-07-06 2015-07-13
1 1 101 2015-07-15 2015-07-22
2 2 102 2015-07-12 2015-07-19
3 2 103 2015-07-07 2015-07-14
4 2 104 2015-07-14 2015-07-21
5 1 105 2015-07-11 2015-07-18 …
Run Code Online (Sandbox Code Playgroud) 我有一个数据帧df,可以用这个创建:
import pandas as pd
import datetime
#create the dates to make into columns
datestart=datetime.date(2018,1,1)
dateend=datetime.date(2018,1,5)
newcols=pd.date_range(datestart,dateend).date
#create the test data
d={'name':['a','b','c','d'],'earlydate': [datetime.date(2018,1,1),datetime.date(2018,1,3),datetime.date(2018,1,4),datetime.date(2018,1,5)]}
#create initial test dataframe
df=pd.DataFrame(data=d)
#create the new dataframe with empty newcols
df=pd.concat([df,pd.DataFrame(columns=newcols)])
Run Code Online (Sandbox Code Playgroud)
并且看起来像这样:
df
Out[17]:
name earlydate 2018-01-01 ... 2018-01-03 2018-01-04 2018-01-05
0 a 2018-01-01 NaN ... NaN NaN NaN
1 b 2018-01-03 NaN ... NaN NaN NaN
2 c 2018-01-04 NaN ... NaN NaN NaN
3 d 2018-01-05 NaN ... NaN NaN NaN
[4 rows …
Run Code Online (Sandbox Code Playgroud)