以下是我的数据框
import pandas as pd
df = pd.DataFrame({'name': ['jon','sam','jane','bob'],
'age': [30,25,18,26],
'sex':['male','male','female','male']})
age name sex
0 30 jon male
1 25 sam male
2 18 jane female
3 26 bob male
Run Code Online (Sandbox Code Playgroud)
我想在第一个位置插入一个新行
姓名:院长,年龄:45岁,性别:男
age name sex
0 45 dean male
1 30 jon male
2 25 sam male
3 18 jane female
4 26 bob male
Run Code Online (Sandbox Code Playgroud)
在熊猫中做到这一点的最佳方法是什么?
我有这样的数据帧:
mainid pidx pidy score
1 a b 2
1 a c 5
1 c a 7
1 c b 2
1 a e 8
2 x y 1
2 y z 3
2 z y 5
2 x w 12
2 x v 1
2 y x 6
Run Code Online (Sandbox Code Playgroud)
我想groupby on column 'pidx' ,然后sort score in descending order in each group即为每个pidx
然后select head(2)是每组的前2名.
我要找的结果是这样的:
mainid pidx pidy score
1 a e 8
1 a c …Run Code Online (Sandbox Code Playgroud) 我有一个数据帧
id store address
1 100 xyz
2 200 qwe
3 300 asd
4 400 zxc
5 500 bnm
Run Code Online (Sandbox Code Playgroud)
我有另一个数据帧df2
serialNo store_code warehouse
1 300 Land
2 500 Sea
3 100 Land
4 200 Sea
5 400 Land
Run Code Online (Sandbox Code Playgroud)
我希望我的最终数据框看起来像:
id store address warehouse
1 100 xyz Land
2 200 qwe Sea
3 300 asd Land
4 400 zxc Land
5 500 bnm Sea
Run Code Online (Sandbox Code Playgroud)
即从一个数据帧映射到另一个数据帧创建新列
我有一张DataFrame名为pandas的桌子df:
+--- -----+------------+-------------+----------+------------+-----------+
|avg_views| avg_orders | max_views |max_orders| min_views |min_orders |
+---------+------------+-------------+----------+------------+-----------+
| 23 | 123 | 135 | 500 | 3 | 1 |
+---------+------------+-------------+----------+------------+-----------+
Run Code Online (Sandbox Code Playgroud)
我现在要寻找的是绘制一个分组条形图,在一个条形图中显示我(平均,最大,最小)的视图和订单.
也就是说,在x轴上会有视图和顺序相隔一段距离和3条(平均值,最大值,最小值)的视图和类似的订单.
我附上了一个示例条形图图像,只是想知道条形图应该如何看.
我从matplotlib中的分组条形图之间设置了以下代码,但它对我不起作用:
plt.figure(figsize=(13, 7), dpi=300)
groups = [[23, 135, 3], [123, 500, 1]]
group_labels = ['views', 'orders']
num_items = len(group_labels)
ind = np.arange(num_items)
margin = 0.05
width = (1. - 2. * margin) / num_items
s = plt.subplot(1, 1, 1)
for num, vals in enumerate(groups): …Run Code Online (Sandbox Code Playgroud) 我正在运行一个变量是pandas.core.series.Series类型的函数.
type of the series shown below.
<class 'pandas.core.series.Series'>
product_id_y 1159730
count 1
Name: 6159402, dtype: object
Run Code Online (Sandbox Code Playgroud)
我想将其转换为数据帧,这样,我得到
product_id_y count
1159730 1
Run Code Online (Sandbox Code Playgroud)
我试过这样做:
series1 = series1.to_frame()
Run Code Online (Sandbox Code Playgroud)
但得到错误的结果
转换为数据帧后
6159402
product_id_y 1159730
count 1
Run Code Online (Sandbox Code Playgroud)
做完重置索引之后我就是 series1 = series1.reset_index()
index 6159402
0 product_id_y 1159730
1 count 1
Run Code Online (Sandbox Code Playgroud)
还有其他方法吗?
我有一个数据帧df:
id name count
1 a 10
2 b 20
3 c 30
4 d 40
5 e 50
Run Code Online (Sandbox Code Playgroud)
这里我有另一个数据帧df2:
id1 price rating
1 100 1.0
2 200 2.0
3 300 3.0
5 500 5.0
Run Code Online (Sandbox Code Playgroud)
我想在列id和id1上加入这两个数据帧(两者都是相同的).这是df3的一个例子:
id name count price rating
1 a 10 100 1.0
2 b 20 200 2.0
3 c 30 300 3.0
4 d 40 Nan Nan
5 e 50 500 5.0
Run Code Online (Sandbox Code Playgroud)
我应该使用df.merge还是pd.concat?
我是Tkinter的新手,
我有一个程序,它将CSV作为输入,包含outlet的地理位置,将其显示在地图上,并将其另存为HTML.
我的csv的格式:
outlet_code Latitude Longitude
100 22.564 42.48
200 23.465 41.65
... and so on ...
Run Code Online (Sandbox Code Playgroud)
下面是我的python代码,用于获取此CSV并将其放在地图上.
import pandas as pd
import folium
map_osm = folium.Map(location=[23.5747,58.1832],tiles='https://korona.geog.uni-heidelberg.de/tiles/roads/x={x}&y={y}&z={z}',attr= 'Imagery from <a href="http://giscience.uni-hd.de/">GIScience Research Group @ University of Heidelberg</a> — Map data © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>')
df = pd.read_excel("path/to/file.csv")
for index, row in df.iterrows():
folium.Marker(location=[row['Latitude'], row['Longitude']], popup=str(row['outlet_code']),icon=folium.Icon(color='red',icon='location', prefix='ion-ios')).add_to(map_osm)
map_osm
Run Code Online (Sandbox Code Playgroud)
这将显示 map_osm
替代方法是保存map_osm为HTML
map_osm.save('path/map_1.html')
Run Code Online (Sandbox Code Playgroud)
我正在寻找的是一个GUI,它将做同样的事情.
即提示用户输入CSV,然后执行下面的代码并显示结果或至少将其保存在某个位置.
任何线索都会有所帮助
我有一个df
id val1 val2
1 1.1 2.2
1 1.1 2.2
2 2.1 5.5
3 8.8 6.2
4 1.1 2.2
5 8.8 6.2
Run Code Online (Sandbox Code Playgroud)
我想分组val1 and val2并获得类似的数据帧,只有多次出现相同val1 and val2组合的行.
最终df:
id val1 val2
1 1.1 2.2
4 1.1 2.2
3 8.8 6.2
5 8.8 6.2
Run Code Online (Sandbox Code Playgroud) 我有一个清单:
L = [1,2,3,4,5,6,7,8]
Run Code Online (Sandbox Code Playgroud)
我想在列表中连续迭代等元素,当它涉及到最后一个元素i'e 8的第一个元素是对1.
我想要的最终输出是:
[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],[7,8],[8,1]
Run Code Online (Sandbox Code Playgroud)
我试过用这种方式:
for first,second in zip(L, L[1:]):
print([first,second])
Run Code Online (Sandbox Code Playgroud)
但我只得到这个结果:
[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],[7,8]
Run Code Online (Sandbox Code Playgroud)
如何先制作一对最后一个元素?我听说过列表的负索引属性.
我在 Pandas df 中有一张桌子
product_id_x product_id_y count
0 2727846 7872456 1
1 29234 2932348 2
2 29346 9137500 1
3 29453 91365738 1
4 2933666 91323494 1
Run Code Online (Sandbox Code Playgroud)
我想添加一个我在 str 中定义的新列“日期”。
dateSelect = "'2016-11-06'"
Run Code Online (Sandbox Code Playgroud)
所以我添加了一个新的常量列
df['dates'] = dateSelect
Run Code Online (Sandbox Code Playgroud)
但我得到的结果是
product_id_x product_id_y count dates
0 2727846 7872456 1 '2016-11-06'
1 29234 2932348 2 '2016-11-06'
2 29346 9137500 1 '2016-11-06'
3 29453 91365738 1 '2016-11-06'
4 2933666 91323494 1 '2016-11-06'
Run Code Online (Sandbox Code Playgroud)
日期中的值用引号引起来。和
type(df['dates']) = str
Run Code Online (Sandbox Code Playgroud)
但我想要日期格式,因为我将进一步将此表存储在我的 mysql 数据库中。我希望类型是日期。
from sqlalchemy import create_engine
engine = …Run Code Online (Sandbox Code Playgroud) python ×10
pandas ×8
python-2.7 ×2
python-3.x ×2
dataframe ×1
join ×1
mapping ×1
matplotlib ×1
tkinter ×1