我在pandas dataframe中有以下数据:
state 1st 2nd 3rd
0 California $11,593,820 $109,264,246 $8,496,273
1 New York $10,861,680 $45,336,041 $6,317,300
2 Florida $7,942,848 $69,369,589 $4,697,244
3 Texas $7,536,817 $61,830,712 $5,736,941
Run Code Online (Sandbox Code Playgroud)
我想用三列(第1,第2,第3)执行一些简单的分析(例如,sum,groupby),但这三列的数据类型是object(或string).
所以我使用以下代码进行数据转换:
data = data.convert_objects(convert_numeric=True)
Run Code Online (Sandbox Code Playgroud)
但是,由于美元符号,转换可能不起作用.有什么建议吗?
我有一个pandas数据帧:
apple banana carrot diet coke
1 1 1 0
0 1 0 0
1 0 0 0
1 0 1 1
0 1 1 0
0 1 1 0
Run Code Online (Sandbox Code Playgroud)
我想将此转换为以下内容:
[['apple', 'banana', 'carrot'],
['banana'],
['apple'],
['apple', 'carrot', 'diet coke'],
['banana', 'carrot'],
['banana', 'carrot']]
Run Code Online (Sandbox Code Playgroud)
我该怎么做?非常感谢.
我有一个数据框:
time year month
0 12/28/2013 0:17 2013 12
1 12/28/2013 0:20 2013 12
2 12/28/2013 0:26 2013 12
3 12/29/2013 0:20 2013 12
4 12/29/2013 0:26 2013 12
5 12/30/2013 0:31 2013 12
6 12/30/2013 0:31 2013 12
7 12/31/2013 0:17 2013 12
8 12/31/2013 0:20 2013 12
9 12/31/2013 0:26 2013 12
10 1/1/2014 4:30 2014 1
11 1/1/2014 4:34 2014 1
12 1/1/2014 4:37 2014 1
13 1/2/2014 4:30 2014 1
14 1/2/2014 5:30 2014 1
15 …Run Code Online (Sandbox Code Playgroud) 我的逗号分隔的txt文件包含以下格式的数据(当然,还有更多的列和行)。
1977,PA,2017-08-07,55,New Firms,327.0
1978,NY,2017-08-07,45,New Firms,$30,127
1978,NY,2017-08-07,$10,000,New Firms,1,000
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,一些数据包含千位分隔符,我读起来是这样的:
df=pd.read_csv("data.txt", thousands=r',')
df.head()
Run Code Online (Sandbox Code Playgroud)
这给出了一条错误消息
ParserError: Error tokenizing data. C error: Expected 13 fields in line 102996, saw 14
Run Code Online (Sandbox Code Playgroud)
我认为发生错误是因为某些数据包含千位分隔符。
有什么建议么?
我正在尝试使用本教程中的Seaborn factorplot 。
以下代码创建一个带有垂直线的条形图。这些竖线代表什么?
sns.factorplot("kind", "pulse", "diet", exercise, kind="bar")
Run Code Online (Sandbox Code Playgroud) 我的数据采用以下格式:
data = [['@datumbox', '#machinelearning'],
['@datumbox', '#textanalysis'],
['@things_internet', '#iot'],
['@things_internet', '#h...'],
['@custmrcom', '#analytics123'],
['@custmrcom', '#strategy...123'],
['@custmrcom', '#1knowledgetweet'],
['@tamaradull', '#@bigbrother']]
Run Code Online (Sandbox Code Playgroud)
我想检查hashtag是否包含任何非字母表.如果是,则删除相应的行.
所需的输出是:
data = [['@datumbox', '#machinelearning'],
['@datumbox', '#textanalysis'],
['@things_internet', '#iot']]
Run Code Online (Sandbox Code Playgroud)
我想我需要使用re.sub(例如,re.compile('[^ a-zA-Z]')).这是我到目前为止:
newdata = []
for item in data:
regex = re.compile('[^a-zA-Z]')
if regex.match(item[1]):
newdata.append([item[0], item[1]])
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?
我正在使用csv.reader和pandas导入一个csv文件。但是,来自同一文件的行数是不同的。
reviews = []
openfile = open("reviews.csv", 'rb')
r = csv.reader(openfile)
for i in r:
reviews.append(i)
openfile.close()
print len(reviews)
Run Code Online (Sandbox Code Playgroud)
结果是10,000(这是正确的值)。但是,熊猫返回不同的值。
df = pd.read_csv("reviews.csv", header=None)
df.info()
Run Code Online (Sandbox Code Playgroud)
这将返回9,985
有谁知道为什么两种导入数据方法之间有区别?
我只是试过这个:
reviews_df = pd.DataFrame(reviews)
reviews_df.info()
Run Code Online (Sandbox Code Playgroud)
这将返回10,000。
我的数据如下所示:
ABC_10_2007_06_14.txt
ABC_11_2008_07_14.txt
Run Code Online (Sandbox Code Playgroud)
我想提取 2007 和 2008。其正则表达式是什么?我在下面尝试过,但它不起作用。
.*_.*_(.*)_.*_.*\..*
Run Code Online (Sandbox Code Playgroud) 我的数据在数据框中。它看起来像这样。
我正在尝试将数据框转换为以下格式:
目前我正在使用以下乏味的 Python 代码进行此转换。
def f(row):
if row['d1'] == 1:
return 1
if row['d1'] == 2:
return ''
if row['d1'] == 3:
return ''
if row['d1'] == 4:
return ''
else:
return ''
return np.nan
df['t1']=df.apply(f, axis=1)
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。
我有一个推文列表。它们看起来像这样:
data = [['trading $aa $BB stock market info'],
['$aa is $116 market is doing well $cc $ABC']]
Run Code Online (Sandbox Code Playgroud)
我想提取股票行情:
['$aa', '$BB']
['$aa', '$cc', '$ABC']]
Run Code Online (Sandbox Code Playgroud)
我试过这个:
for i in data:
print re.findall(r'[$]\S*', str(i))
Run Code Online (Sandbox Code Playgroud)
而且,输出还包含 $116:
['$aa', '$BB']
['$aa', '$116', '$cc', '$ABC']]
Run Code Online (Sandbox Code Playgroud)
有什么建议?