我有以下for循环:
for i in links:
data = urllib2.urlopen(str(i)).read()
data = json.loads(data)
data = pd.DataFrame(data.items())
data = data.transpose()
data.columns = data.iloc[0]
data = data.drop(data.index[[0]])
Run Code Online (Sandbox Code Playgroud)
如此创建的每个数据框都有大多数列与其他列相同但不是全部.而且,他们都只有一排.我需要的是向数据帧中添加for循环生成的每个数据帧中的所有不同列和每一行
我尝试过连接或类似的熊猫,但似乎没有任何效果.任何的想法?谢谢.
我需要遍历pandas df的每一行并将其转换为逗号分隔的字符串.
例:
df3 = DataFrame(np.random.randn(10, 5),
columns=['a', 'b', 'c', 'd', 'e'])
a b c d e
0 -0.158897 -0.749799 0.268921 0.070035 0.099600
1 -0.863654 -0.086814 -0.614562 -1.678850 0.980292
2 -0.098168 0.710652 -0.456274 -0.373153 -0.533463
3 1.001634 -0.736187 -0.812034 0.223062 -1.337972
4 0.173549 -0.576412 -1.016063 -0.217242 0.443794
5 0.273695 0.335562 0.778393 -0.668368 0.438880
6 -0.783824 1.439888 1.057639 -1.825481 -0.770953
7 -1.025004 0.155974 0.645023 0.993379 -0.812133
8 0.953448 -1.355628 -1.918317 -0.966472 -0.618744
9 -0.479297 0.295150 -0.294449 0.679416 -1.813078
Run Code Online (Sandbox Code Playgroud)
我想得到每一行:
'-0.158897,-0.749799,0.268921,0.070035,0.099600'
'0.863654,-0.086814,-0.614562,-1.678850,0.980292' …Run Code Online (Sandbox Code Playgroud) 我有一组包含json文件的URL和一个空的pandas数据框,其中的列表示jsnon文件的属性.并非所有json文件都具有pandas数据帧中的所有属性.我需要做的是从json文件中创建字典,然后将每个字典作为新行追加到pandas数据帧中,如果json文件没有与数据帧中的列匹配的属性,则必须填空白.
我设法创建了词典:
import urllib2
import json
url = "https://cws01.worldstores.co.uk/api/product.php?product_sku=ULST:7BIS01CF"
data = urllib2.urlopen(url).read()
data = json.loads(data)
Run Code Online (Sandbox Code Playgroud)
然后我尝试创建一个for循环,如下所示:
row = -1
for i in links:
row = row + 1
data = urllib2.urlopen(str(i)).read()
data = json.loads(data)
for key in data.keys():
for column in df.columns:
if str(column) == str(key):
df.loc[[str(column)],row] = data[str(key)]
else:
df.loc[[str(column)],row] = None
Run Code Online (Sandbox Code Playgroud)
其中df是数据帧,链接是url的集合
但是,我收到以下错误:
raise KeyError('%s not in index' % objarr[mask])
KeyError: "['2_seater_depth_mm'] not in index"
Run Code Online (Sandbox Code Playgroud)
其中['2_seater_depth_mm']是pandas数据帧的第一列
我有以下数据帧:
0 1
0 enrichment_site value
1 last_updated value
2 image_names value
3 shipping_weight value
4 ean_gtin value
5 stockqty value
6 height__mm value
7 availability value
8 rrp value
9 sku value
10 price_band value
11 item value
Run Code Online (Sandbox Code Playgroud)
我尝试使用数据透视表
test.pivot(index=index, columns='0', values='1')
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
KeyError: '1'
Run Code Online (Sandbox Code Playgroud)
使用数据透视表的任何替代方法吗?
我正在尝试替换 GITHUB 操作步骤中变量中的字符
- name: Set Up DB Name
run: |
DB_NAME="${GITHUB_REF_SLUG/-/_}"
echo $DB_NAME
Run Code Online (Sandbox Code Playgroud)
我收到了错误的请求错误
我究竟做错了什么?
我正在尝试使用以下函数重塑我的pd数据帧:
ar = ar.pivot(index='Received', columns='Merch Ref', values='acceptance_rate')
Run Code Online (Sandbox Code Playgroud)
数据集如下所示:
Merch Ref Received acceptance_rate
0 SF 2014-08-28 15:38:00 0
1 SF 2014-08-28 15:44:00 0
2 SF 2014-08-28 16:04:00 0
3 WF 2014-08-28 16:05:00 0
4 WF 2014-08-28 16:07:00 0
5 SF 2014-08-28 16:34:00 0
6 SF 2014-08-28 16:55:00 0
7 BF 2014-08-28 17:59:00 0
8 BF 2014-08-29 15:05:00 0
9 SF 2014-08-29 21:25:00 0
10 SF 2014-08-30 10:29:00 0
...
Run Code Online (Sandbox Code Playgroud)
我想得到的是:
SF WF BF
2014-08-28 15:38:00 0 1 0
2014-08-28 15:44:00 0 …Run Code Online (Sandbox Code Playgroud) 我在 pandas 数据框中有一列,日期为:
年、日、月、时、分、秒
2015-09-03 14:32:00
Run Code Online (Sandbox Code Playgroud)
我想把它变成
年、月、日、时、分、秒
2015-03-09 14:32:00
Run Code Online (Sandbox Code Playgroud) 我在 ES 中有这个文档,我正在通过 Kibana 查看它。
_score 字段代表什么?
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 11,
"successful": 11,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "order",
"_type": "ACKNOWLEDGED",
"_id": "9901234567",
"_score": 0.2876821,
"_source": {
"applicationCode": "SAPS00",
"orderId": "9901234567",
"status": "ACKNOWLEDGED",
"orderUpdatedDateTime": "2018-07-08T10:12:21Z",
"totals": {
"orderShippingTaxAmount": 3.5,
"orderSubtotalTaxAmount": 12.55,
"grandTotalTaxAmount": 15
},
"orderLines": [
{
"lineId": "1",
"unitPriceTaxAmount": 5.45,
"totalPriceTaxAmount": 10.67,
"lineShippingTaxAmount": null
},
{
"lineId": "2",
"unitPriceTaxAmount": 2.45,
"totalPriceTaxAmount": 8.67,
"lineShippingTaxAmount": null …Run Code Online (Sandbox Code Playgroud) 我有一个像以下列的pandas数据框:
In [96]: data['difference']
Out[96]:
0 NaT
1 1 days 21:34:30
2 0 days 16:57:36
3 0 days 00:16:51
4 0 days 15:52:38
5 0 days 14:19:34
6 0 days 02:54:46
7 1 days 04:21:28
8 0 days 01:58:55
9 0 days 10:30:35
10 0 days 07:53:04
....
Name: difference, dtype: timedelta64[ns]
Run Code Online (Sandbox Code Playgroud)
我想在它旁边创建一个列,该列的整数对应于此列中的days值.
我试图增加由此功能产生的图像的大小:
plt.figure()); data_ordertotal.plot(); plt.legend(loc='best')
Run Code Online (Sandbox Code Playgroud)
我试过这个,但尺寸保持不变
plt.figure(figsize=(40,40)); data_ordertotal.plot(); plt.legend(loc='best')
Run Code Online (Sandbox Code Playgroud)
我使用spyder进行编码,控制台中的输出始终保持相同的大小.有解决方案吗 谢谢
pandas ×7
python ×7
dataframe ×3
for-loop ×2
datetime ×1
dictionary ×1
json ×1
kibana ×1
matplotlib ×1
pivot ×1
pivot-table ×1
replace ×1
timedelta ×1