我有一种情况,有时当我读取一个csv来自df我得到一个不需要的索引列名称unnamed:0.这很烦人!我试过了
,A,B,C
0,1,2,3
1,4,5,6
2,7,8,9
Run Code Online (Sandbox Code Playgroud)
我认为这是一个解决方案,但我仍然得到file.csv专栏!有没有人对此有所了解?
我正在使用以下df:
c.sort_values('2005', ascending=False).head(3)
GeoName ComponentName IndustryId IndustryClassification Description 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
37926 Alabama Real GDP by state 9 213 Support activities for mining 99 98 117 117 115 87 96 95 103 102 (NA)
37951 Alabama Real GDP by state 34 42 Wholesale trade 9898 10613 10952 11034 11075 9722 9765 9703 9600 9884 10199
37932 Alabama Real GDP by state 15 327 Nonmetallic mineral products manufacturing 980 968 940 …Run Code Online (Sandbox Code Playgroud) 我有以下数据帧:
amount catcode cid cycle date di feccandid type
0 1000 E1600 N00029285 2014 2014-05-15 D H8TX22107 24K
1 5000 G4600 N00026722 2014 2013-10-22 D H4TX28046 24K
2 4 C2100 N00030676 2014 2014-03-26 D H0MO07113 24Z
Run Code Online (Sandbox Code Playgroud)
我想为列中的值创建虚拟变量type.大约15岁.我试过这个:
pd.get_dummies(df['type'])
它返回:
24A 24C 24E 24F 24K 24N 24P 24R 24Z
date
2014-05-15 0 0 0 0 1 0 0 0 0
2013-10-22 0 0 0 0 1 0 0 0 0
2014-03-26 0 0 0 0 0 0 0 0 …Run Code Online (Sandbox Code Playgroud) 我试图检查python列中是否包含某个值.我正在使用df.date.isin(['07311954']),我不怀疑它是一个很好的工具.问题是我有超过350K的行,输出不会显示所有这些,所以我可以看到该值是否实际包含.简而言之,我只想知道(Y/N)列中是否包含特定值.我的代码如下:
import numpy as np
import pandas as pd
import glob
df = (pd.read_csv('/home/jayaramdas/anaconda3/Thesis/FEC_data/itpas2_data/itpas214.txt',\
sep='|', header=None, low_memory=False, names=['1', '2', '3', '4', '5', '6', '7', \
'8', '9', '10', '11', '12', '13', 'date', '15', '16', '17', '18', '19', '20', \
'21', '22']))
df.date.isin(['07311954'])
Run Code Online (Sandbox Code Playgroud) 我有以下两个数据帧,我已将日期设置为DateTime Index,df.set_index(pd.to_datetime(df['date']), inplace=True)并希望在日期合并或加入:
df.head(5)
catcode_amt type feccandid_amt amount
date
1915-12-31 A5000 24K H6TX08100 1000
1916-12-31 T6100 24K H8CA52052 500
1954-12-31 H3100 24K S8AK00090 1000
1985-12-31 J7120 24E H8OH18088 36
1997-12-31 z9600 24K S6ND00058 2000
d.head(5)
catcode_disp disposition feccandid_disp bills
date
2007-12-31 A0000 support S4HI00011 1
2007-12-31 A1000 oppose S4IA00020', 'P20000741 1
2007-12-31 A1000 support S8MT00010 1
2007-12-31 A1500 support S6WI00061 2
2007-12-31 A1600 support S4IA00020', 'P20000741 3
Run Code Online (Sandbox Code Playgroud)
我尝试了以下两种方法但都返回了一个MemoryError:
df.join(d, how='right')
Run Code Online (Sandbox Code Playgroud)
我在没有将日期设置为索引的数据帧上使用下面的代码.
merge=pd.merge(df,d, how='inner', on='date')
Run Code Online (Sandbox Code Playgroud) 我正在使用Python; 我需要遍历JSON对象并检索嵌套值.我的数据片段如下:
"bills": [
{
"url": "http:\/\/maplight.org\/us-congress\/bill\/110-hr-195\/233677",
"jurisdiction": "us",
"session": "110",
"prefix": "H",
"number": "195",
"measure": "H.R. 195 (110\u003csup\u003eth\u003c\/sup\u003e)",
"topic": "Seniors' Health Care Freedom Act of 2007",
"last_update": "2011-08-29T20:47:44Z",
"organizations": [
{
"organization_id": "22973",
"name": "National Health Federation",
"disposition": "support",
"citation": "The National Health Federation (n.d.). \u003ca href=\"http:\/\/www.thenhf.com\/government_affairs_federal.html\"\u003e\u003ccite\u003e Federal Legislation on Consumer Health\u003c\/cite\u003e\u003c\/a\u003e. Retrieved August 6, 2008, from The National Health Federation.",
"catcode": "J3000"
},
{
"organization_id": "27059",
"name": "A Christian Perspective on Health Issues",
"disposition": "support",
"citation": "A Christian …Run Code Online (Sandbox Code Playgroud) 我有以下数据帧:
df = pd.read_csv('https://raw.githubusercontent.com/108michael/ms_thesis/master/crsp.dime.mpl.df')
df.groupby('date').cid.size()
date
2005 7
2006 237
2007 3610
2008 1318
2009 2664
2010 997
2011 6390
2012 2904
2013 7875
2014 3979
df.groupby('date').cid.nunique()
date
2005 3
2006 10
2007 227
2008 52
2009 142
2010 57
2011 219
2012 99
2013 238
2014 146
Name: cid, dtype: int64
Run Code Online (Sandbox Code Playgroud)
我想消除重复的cid值,使输出df.groupby('date').cid.size()匹配输出df.groupby('date').cid.nunique().我看过这篇文章,但它似乎没有一个可靠的解决方案.
我尝试过以下方法:
df = pd.read_csv('https://raw.githubusercontent.com/108michael/ms_thesis/master/crsp.dime.mpl.df')
df.groupby('date').cid.size()
date
2005 7
2006 237
2007 3610
2008 1318
2009 2664
2010 997
2011 …Run Code Online (Sandbox Code Playgroud) 我在ipython工作; 我有一个Yaml文件和一个与我的Yaml文件对应的[thomas] id列表(托马斯:文件中的第三行).下面只是该文件的一小部分.完整的文件可以在这里找到(https://github.com/108michael/congress-legislators/blob/master/legislators-historical.yaml)
- id:
bioguide: C000858
thomas: '00246'
lis: S215
govtrack: 300029
opensecrets: N00002091
votesmart: 53288
icpsr: 14809
fec:
- S0ID00057
wikipedia: Larry Craig
house_history: 11530
name:
first: Larry
middle: E.
last: Craig
bio:
birthday: '1945-07-20'
gender: M
religion: Methodist
terms:
- type: rep
start: '1981-01-05'
end: '1983-01-03'
state: ID
district: 1
party: Republican
- type: rep
start: '1983-01-03'
end: '1985-01-03'
state: ID
district: 1
party: Republican
Run Code Online (Sandbox Code Playgroud)
我想解析文件和我的列表中与[thomas:]中的Id对应的每个id我要检索以下内容:[fec] :(可能有多个这些,我需要所有这些) [name:] [first:] [middle:] [last:]; [生物:] [生日:]; [条款:](可能有不止一个术语,我需要所有术语)[type:] [start:] …
我试图返回一个包含所有NaN值的df,column == years_exp以便我可以识别相应的值id.thomas(基本上,我正在调试一些我手工分析的数据)。我还需要返回具有所有min值的df 。到目前为止,这是我尝试过的:
rr.head(5)
years id.thomas years_exp
55 2005 2 17
56 2006 2 18
57 2007 2 19
58 2008 2 20
59 2009 2 21
c = rr
c = c[c.years_exp == 'NaN']
Run Code Online (Sandbox Code Playgroud)
错误:
TypeError:无效的类型比较
我使用的是从Pandas上的youtube视频复制的语法。有人对这个错误有想法吗?
我目前正在使用from pandas.stats.plm import PanelOLS运行面板回归。我需要切换到 statsmodel 以便我可以输出异方差稳健的结果。我一直无法找到有关为 statsmodel 调用面板回归的符号。总的来说,我发现 statsmodel 的文档不是很用户友好。有人熟悉 statsmodel 中的面板回归语法吗?
pandas ×9
python ×9
dataframe ×3
csv ×1
duplicates ×1
for-loop ×1
ipython ×1
json ×1
python-3.x ×1
statsmodels ×1
unique ×1